From 42e88e4dc65f6b2ab1d8fa6a1974fc7b795631cc Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Thu, 11 Apr 2024 16:59:32 +0000 Subject: [PATCH 1/1] feat(user/mp4_make480.sh):Use GNU parallel, split into functions --- user/mp4_make480.sh | 56 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/user/mp4_make480.sh b/user/mp4_make480.sh index 8fc75ed..abe5120 100644 --- a/user/mp4_make480.sh +++ b/user/mp4_make480.sh @@ -1,19 +1,49 @@ #!/bin/bash # Desc: Reëncodes .mp4 videos in a specified directory. -# Usage: mp4_make480.sh [dir] +# Usage: mp4_make480.sh [dir in] [dir out] +# Input: arg1 dir input dir +# arg2 dir output dir (optional) # Output: dir ./out/ # file ./out/[files]_480.mp4 -# Version: 0.0.1 +# Version: 0.1.0 +# Depends: GNU parallel 20161222, ffmpeg 4.3.6-0 +# Ref/Attrb: [1]: FFmpeg wiki: https://trac.ffmpeg.org/wiki/Scaling +declare -g dir_in dir_out; -dir_in="$1"; -dir_out="./out"; -if [[ $# -ne 1 ]]; then echo "FATAL:No dir specified." 1>&2; exit 1; fi; -if [[ ! -d "$dir_in" ]]; then echo "FATAL:Not a dir:$dir_in" 1>&2; exit 1; fi; -mkdir -p "$dir_out"; -declare -p dir_in dir_out; # debug +check_input() { + dir_in="$1"; + if [[ $# -le 0 ]]; then echo "FATAL:Insufficient args:$#" 1>&2; exit 1; fi; + if [[ $# -eq 2 ]]; then + dir_out="$2"; + else + dir_out="./out_480"; + fi; + export dir_out; + if [[ ! -d "$dir_in" ]]; then echo "FATAL:Not a dir:$dir_in" 1>&2; exit 1; fi; + mkdir -p "$dir_out"; + declare -p dir_in dir_out; # debug +}; +convert_video() { + find "$dir_in" -mindepth 1 -maxdepth 1 -type f -name "*.mp4" | \ + parallel job_ffmpeg "{}" "$path_out"; +}; +job_ffmpeg() { + path_in="$1"; + path_out="$2"; + file_in="$(basename "$path_in")"; + path_out=./"$dir_out"/"${file_in%.mp4}"_480.mp4; + opt_scale="scale=-2:480"; # See [1] + declare -p path_in path_out file_in dir_out path_out opt_scale; + ffmpeg -nostdin -i "$path_in" -vf "$opt_scale" -c:a copy "$path_out" 1>/dev/random 2>&1; +}; +export -f job_ffmpeg; -while read -r line; do - path_out=./"$dir_out"/"${line%.mp4}"_480.mp4; - declare -p line path_out; # debug - ffmpeg -nostdin -i "$dir_in" -vf "scale=-2:480" -c:a copy "$path_out"; -done < <(find . -mindepth 1 -maxdepth 1 -type f -name "*.mp4") +main() { + check_input "$@"; + convert_video; +}; + +main "$@"; + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+ -- 2.30.2