]> zdv2.bktei.com Git - BK-2020-03.git/blob - user/mp4_make_small.sh
feat(user/mp4_make_small.sh):Do not assume din is pwd
[BK-2020-03.git] / user / mp4_make_small.sh
1 #!/bin/bash
2 # Desc: Reèˆncodes .mp4 videos in a specified directory to reduce size.
3 # Usage: mp4_make480.sh [dir in] [dir out]
4 # Input: arg1 dir input dir
5 # arg2 dir output dir (optional)
6 # Output: dir ./out/
7 # file ./out/[files]_small.mp4
8 # Version: 0.0.3
9 # Depends: GNU parallel 20231122, ffmpeg 6.1.1
10 # Ref/Attrb:
11 declare -g dir_in dir_out;
12
13 check_input() {
14 dir_in="$1";
15 if [[ ! -d "$dir_in" ]]; then echo "FATAL:Not a dir:$dir_in" 1>&2; exit 1; fi;
16 if [[ $# -le 0 ]]; then echo "FATAL:Insufficient args:$#" 1>&2; exit 1; fi;
17 if [[ $# -eq 2 ]]; then
18 dir_out="$2";
19 else
20 dir_out="${dir_in}/out_small";
21 fi;
22 export dir_out;
23
24 mkdir -p "$dir_out";
25 declare -p dir_in dir_out; # debug
26 };
27 convert_video() {
28 find "$dir_in" -mindepth 1 -maxdepth 1 -type f -iname "*.mp4" | \
29 parallel -j25% job_ffmpeg "{}" "$path_out";
30 };
31 job_ffmpeg() {
32 path_in="$1";
33 path_out="$2";
34 file_in="$(basename "$path_in")";
35 path_out="${dir_out}/${file_in%.[mM][pP]4}"_small.mp4;
36
37 declare -p path_in path_out file_in dir_out path_out;
38 ffmpeg -nostdin -i "$path_in" -c:v libx264 -preset veryslow -crf 26 -pix_fmt yuv420p "$path_out";
39 };
40 export -f job_ffmpeg;
41
42 main() {
43 SECONDS=0;
44 ti="$(date -Is)";
45 check_input "$@";
46 convert_video;
47 tf="$(date -Is)";
48 printf "%s\n" "STATUS:Finished in $SECONDS seconds with status $?.";
49 printf "%s\n%s\n%s\n" "STATUS:Job start and end times:" "$ti" "$f";
50 };
51
52 main "$@";
53
54 # Author: Steven Baltakatei Sandoval
55 # License: GPLv3+