Last active
March 24, 2021 13:16
-
-
Save karrirasinmaki/33fefbd9a8f9fae3e7e599855bc91327 to your computer and use it in GitHub Desktop.
Bash script to generate proxies from videos and audios.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# helpful resource: http://macilatthefront.blogspot.com/2018/12/tutorial-using-ffmpeg-for-dnxhddnxhr.html | |
# Usage: | |
# proxies.sh -t audio -t video -s ./media-path -p ./proxy-path | |
quality="lb" | |
codec="dnxhr" | |
suffix_video="${SUFFIX_VIDEO:-mxf}" | |
suffix_audio="wav" | |
width="" | |
proxy_path="./proxy" | |
media_path="./media" | |
input_file="" | |
output_file="" | |
units=4 | |
CONVERT_VIDEO=0 | |
CONVERT_AUDIO=0 | |
DRY=0 | |
cmd_ffmpeg="ffmpeg -loglevel warning -hwaccel auto -y -noautorotate" | |
# | |
cmd_params_video="\ | |
$PARAMS_FFMPEG \ | |
-g 1 \ | |
" | |
# -sn -an \ disable subtitles and audio | |
# -vf colormatrix=bt601:bt709 \ | |
function show_help { | |
echo "Usage: [env_variables] ./proxies.sh [options]" | |
echo "" | |
echo "Environment variables:" | |
echo "SUFFIX_VIDEO=mxf" | |
echo "PARAMS_FFMPEG=extra ffmpeg params" | |
echo "" | |
echo "Options:" | |
echo "-d dry -- run without actual processing" | |
echo "-i single media input to convert" | |
echo "-o output file if using -i option" | |
echo "-s ./media-source-folder -- default=$media_path" | |
echo "-p ./proxy-folder -- default=$proxy_path" | |
echo "-t audio|video -- select what type of media to process, can be repeated, default=none" | |
echo "-c codec dnxhr|h264_nvenc -- default=$codec" | |
echo "-w width to scale -- default=-1 (-1 no scaling)" | |
echo "-q lb|sq|hq -- quality for proxies, default=$quality" | |
echo "-u max units, how many videos/units can be processed at the same time -- default=$units" | |
echo "" | |
} | |
OPTIND=1 | |
while getopts "ds:p:t:q:c:w:u:i:o:h": opt; do | |
case "$opt" in | |
h|\?) | |
show_help | |
exit 0 | |
;; | |
d) | |
DRY=1 | |
;; | |
t) | |
[[ "$OPTARG" == "video" ]] && CONVERT_VIDEO=1 | |
[[ "$OPTARG" == "audio" ]] && CONVERT_AUDIO=1 | |
;; | |
q) | |
quality="$OPTARG" | |
;; | |
c) | |
codec="$OPTARG" | |
;; | |
w) | |
width="$OPTARG" | |
cmd_params_video="$cmd_params_video -vf scale=$width:-1" | |
;; | |
s) | |
media_path="$OPTARG" | |
;; | |
p) | |
proxy_path="$OPTARG" | |
;; | |
i) | |
input_file="$OPTARG" | |
;; | |
o) | |
output_file="$OPTARG" | |
;; | |
u) | |
units="$OPTARG" | |
;; | |
esac | |
done | |
if [ $OPTIND -eq 1 ]; then | |
echo "No options were passed." | |
show_help | |
exit 0 | |
fi | |
params=( "$@" ) | |
function convertVideoCmd { | |
mkdir -p ${proxy_path} | |
filepath=$1 | |
filename=$(basename $filepath) | |
outpath=$2 | |
if [[ -z "$outpath" ]]; then | |
outpath="${proxy_path}/${filename%.*}-proxy-${codec}_${quality}_${width}.${suffix_video}" | |
fi | |
if [[ -f "$outpath" ]]; then | |
# echo "Skipping $filepath; proxy exists." | |
echo "" | |
exit 0 | |
fi | |
cmd="$cmd_ffmpeg -i $filepath" | |
if [[ "$codec" == "h264_nvenc" ]]; then | |
cmd="$cmd -c:v h264_nvenc -preset fast -pix_fmt yuv420p" | |
if [[ "$quality" == "lb" ]]; then cmd="$cmd -qp 12"; | |
elif [[ "$quality" == "sq" ]]; then cmd="$cmd -qp 24"; | |
fi | |
else | |
cmd="$cmd -c:v dnxhd -profile:v dnxhr_$quality -pix_fmt yuv422p" | |
fi | |
cmd="$cmd $cmd_params_video -c:a pcm_s16le $outpath" | |
echo "$cmd" | |
} | |
function convertAudio { | |
echo " converting $filepath" | |
mkdir -p ${proxy_path} | |
filepath=$1 | |
filename=$(basename $filepath) | |
echo "ffmpeg -i $filepath -c:a pcm_s16le ${proxy_path}/${filename%.*}-proxy.${suffix_audio}" | |
} | |
if [[ -f "$input_file" ]]; then | |
echo "Converting $input_file" | |
cmd="$(convertVideoCmd "$input_file" "$output_file")" | |
echo -e "$cmd" | |
if [[ $DRY == 0 ]]; then | |
echo -e "$cmd" |xargs --verbose --max-procs="$units" -I CMD bash -c CMD | |
fi | |
wait | |
echo "done." | |
fi | |
if [[ $CONVERT_VIDEO == 1 ]]; then | |
echo "Converting videos..." | |
cmds="" | |
for file in ${media_path}/*.{MOV,mov,mkv,mp4}; do | |
echo "$file" | |
if [[ -f "$file" ]]; then | |
cmds="$cmds\n$(convertVideoCmd "$file")" | |
fi | |
done | |
echo -e "$cmds" | |
if [[ $DRY == 0 ]]; then | |
echo -e "$cmds" |xargs --verbose --max-procs="$units" -I CMD bash -c CMD | |
fi | |
wait | |
echo "done." | |
fi | |
if [[ $CONVERT_AUDIO == 1 ]]; then | |
echo "Converting audio..." | |
cmds="" | |
for file in ${media_path}/*.{aac,mp3,wav,flac}; do | |
cmds="$cmds\n$(convertAudio "$file")" | |
done | |
echo -e "$cmds" | |
if [[ $DRY == 0 ]]; then | |
echo -e "$cmds" |xargs --verbose --max-procs="$units" -I CMD bash -c CMD | |
fi | |
wait | |
echo "done." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment