2

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 :)

QWEbie
  • 35

1 Answers1

3

Set this shopt -s globstar in your script change your for loop to;

shopt -s globstar
for video_file in ./**/*.mp4;

To see what can be switched on or off or set in bash script so:

shopt

autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    on
cmdhist         on
compat31        off
compat32        off
compat40        off
compat41        off
compat42        off
compat43        off
complete_fullquote  on
direxpand       off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         on
extquote        on
failglob        off
force_fignore   on
globasciiranges off
globstar        on
gnu_errfmt      off
histappend      on
histreedit      off
histverify      off
hostcomplete    off
huponexit       off
inherit_errexit off
interactive_comments    on
lastpipe        off
lithist         off
login_shell     off
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell    off
shift_verbose   off
sourcepath      on
xpg_echo        off
George Udosen
  • 36,677
  • wow thanks! this worked. My 2nd questioned also answered by George Udosen with a 2 liner! What did I spend my time on researching the "find" for =| – QWEbie Aug 17 '18 at 15:38