I am trying to write a script to check video file codec (using ffprobe), and encode them accordingly (with HandBrakeCLI).
Directory
mp4
>> Sub-Directory
>> mp4
>> >> Sub-sub Directory
>> >> mp4
One of the problems in the script is that it can't do recursive sub directories. I tried to change the
for video_file in $(find . -type f -name '*.mp4')
the line above is probably wrong as I was testing afew of the codes by reading AskUbuntu and trying them out. Can't remember which got it to work, but in the end it was unable to deal with whitespaces in files/directories.
The script that works for current working directory, but not recursive directories:
#!/bin/bash
for video_file in ./*.mp4;
do
## using ffprobe to check video codec
CHECK_FILE=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$video_file"`
if [ $CHECK_FILE = hevc ]
then
echo "$video_file is hevc/h265, skipping"
else
# remove .mp4 extension
tempname="${video_file%.*}"_tmp.mp4
newname="${video_file%.*}"_hevc.mp4
HandBrakeCLI -i "$video_file" -o "$tempname"
mv -v "$tempname" "$newname"
echo "Moving on to next available file"
fi
done
Appreciate it if someone could show the way :)
shopt -s globstar
and afor video_file in **/*.mp4
should be enough – muru Aug 17 '18 at 15:19