bash script variables not proper

Posted on

Problem :

I updated it a little bit, but I’m still not getting the results I should be getting:

find "$1" -type f | while read filename

    videoCodec=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$filename")
    audioCodec=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$filename")
    audioChannels=$(ffprobe -v error -select_streams a:0 -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$filename")

    echo $videoCodec $audioCodec $audioChannels $filename

    if [ $videoCodec = "h264" ] && [ $audioCodec = "aac" ] && [ audioChannels = "2" ] ; then
        echo "Direct play capable"


I run this on a folder containing a file that meets all the if conditions (“h264 aac 2 ./Black.Mass.(2015)/Black.Mass.(2015).mp4”) but do not get the “Direct play capable” echo.

Solution :

You don’t say, but is it supposed to not run for DTS audio? In any case, this line won’t work right:

if [[ $video_good="true" && $audio_good="true" ]] ; then

Since there are no spaces around the equal signs, they’re parsed as part of strings rather than as comparison operators. Use this instead:

    if [[ $video_good = "true" && $audio_good = "true" ]] ; then

(Double-equal signs, like you use in the earlier comparisons, would also work. But again, spaces around them are required.)

BTW, I strongly recommend double-quoting variable references (i.e. "$f" instead of just $f). But the way you’re using $cli_video and $cli_audio wouldn’t work if they were double-quoted, so don’t do it to them. The really proper way to store parameters like that is to use arrays instead of plain variables, like this:

    cli_video=(-c:v copy)
    cli_audio=(-c:a copy)
    ffmpeg -i "$f" "${cli_video[@]}" "${cli_audio[@]}" "$(basename "$f")"

Leave a Reply

Your email address will not be published. Required fields are marked *