Skip to content

Instantly share code, notes, and snippets.

@Beomi
Created January 19, 2016 06:11
Show Gist options
  • Save Beomi/e4f14ddab9038357d98b to your computer and use it in GitHub Desktop.
Save Beomi/e4f14ddab9038357d98b to your computer and use it in GitHub Desktop.
encoding AVI to MP4 shell script
#!/bin/sh
ffprobe="/volume1/NAS/SH/dts/ffprobe"
ffmpeg="/volume1/NAS/SH/dts/ffmpeg"
cachedir="/volume1/NAS/SH/dts"
## cleanup by deleting unwanted files
find $1 -name "*.txt" -type f -print0 | xargs -0 rm -rf
find $1 -name "*.db" -type f -print0 | xargs -0 rm -rf
##find $1 -name "*.nfo" -type f -print0 | xargs -0 rm -rf
find $1 -name "*sample*.mkv" -type f -print0 | xargs -0 rm -rf
find $1 -name "*Sample*.mkv" -type f -print0 | xargs -0 rm -rf
find $1 -name "*SAMPLE*.mkv" -type f -print0 | xargs -0 rm -rf
## go over all MKVs
find $1 -name "*.avi" -type f | while read f
##find $1 -name "*.avi" -type f | for ((a=0,a<10;a++))
##find $1 -name "*.mkv" -type f | while read f
do
echo =================================================================================
echo processing $f
## check lock
echo "checking for ""${f%\/*}"/lock""
if [ -f ""${f%\/*}"/lock" ];
then
echo "file is locked. skipping."
continue
fi
## create lock
touch ""${f%\/*}"/lock"
## uncomment following 2 lines for subliminal call
## echo -e "executing subs /volume1/@appstore/subliminal/env/bin/subliminal -f -l eng ${f%\/*}/"
## /volume1/@appstore/subliminal/env/bin/subliminal --cache-dir $cachedir -f -l eng ""${f%\/*}"/"
## Detect what audio codec is being used:
audio=$($ffprobe "$f" 2>&1 | sed -n '/Audio:/s/.*: \([a-zA-Z0-9]*\).*/\1/p' | sed 1q)
streams=$($ffprobe "$f" 2>&1 | sed -n 's/.*Stream #0:\([0-9]*\).*/\1/p' | tail -n 1)
## Set default video settings:
##vopts="-c:v copy"
##vopts="-c:v libx264 -crf 19 -preset slow"
vopts="-c:v libx264 -crf 21 -preset slow"
## Set default subtitle settings:
sopts="-c:s mov_text"
## others: copy all
eopts="-map 0 -c copy"
echo "-----> audio is $audio"
echo "-----> there are $(($streams+1)) streams"
case "$audio" in
"" )
## If there is no detected audio stream, don't bother
echo "-----> can't determine audio, skipping"
;;
* )
## anything else, convert
case "$audio" in
## AC3: keep original, add ACC
ac3 )
aopts="-map 0:a -c:a:1 copy -c:a:0 aac -ac:a:0 2 -strict -2"
;;
## anything else, put AC3 and AAC in place
* )
aopts="-map 0:a -c:a:1 ac3 -b:a:1 640k -c:a:0 aac -ac:a:0 2 -strict -2"
;;
esac
##echo -e "-----> executing $ffmpeg -y -i $f $eopts $sopts $vopts $aopts ${f%.mkv}.mp4"
echo -e "-----> executing $ffmpeg -y -i $f $eopts $sopts $vopts $aopts ${f%.avi}.mp4"
##$ffmpeg -nostdin -y -i "$f" $eopts $sopts $vopts $aopts "${f%.mkv}.mp4" > /dev/null 2>&1
$ffmpeg -nostdin -y -i "$f" $eopts $sopts $vopts $aopts -ss 5 -t 5 "${f%.avi}.mp4" > /dev/null 2>&1
fail=$?
case "$fail" in
"0" )
## put new file in place
echo "-----> SUCCES"
##rm -rf "$f"
##synoindex -D "$f"
##chmod 666 "${f%.mkv}.mp4"
chmod 666 "${f%.avi}.mp4"
##synoindex -A "${f%.mkv}.mp4"
synoindex -A "${f%.avi}.mp4"
;;
* )
echo "-----> FAIL"
## revert back
##rm -rf "${f%.mkv}.mp4"
rm -rf "${f%.avi}.mp4"
;;
esac
;;
esac
## release lock
rm -rf ""${f%\/*}"/lock"
##done
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment