Question

I wrote a shell script to convert many video files and save them with something appended to the file name. The script works, but it seems to randomly skip files, and a lot of them.

When I re-run the script, it will convert files it skipped before. How can I get it to stop skipping files?

workingDir=/home/user/Videos

# get list of files to convert
find /video/folder -iname "*.mp4" > $workingDir/file_list

# convert files
cat $workingDir/file_list | while read LINE; do
    # FFmpeg often cuts off the beginning of this line
    echo "$(dirname "$LINE")/$(basename "$LINE")"
    if /usr/bin/ffmpeg -n -loglevel panic -v quiet -stats -i "$LINE" \
        -c:v libx264 -vf scale="trunc(oh*a/2)*2:320" \
        -pix_fmt yuv420p -preset:v slow -profile:v main -tune:v animation -crf 23 \
        "$(dirname "$LINE")/$(basename "$LINE" \.mp4)"_reencoded.mp4 2>/dev/null; then
            echo "Success: $(dirname "$LINE")/$(basename "$LINE")" >> $workingDir/results
    else
        echo "Failed:  $(dirname "$LINE")/$(basename "$LINE")" >> $workingDir/results
    fi
done

One problem seems to be that FFmpeg interferes with the script. The FFmpeg output often cuts off the beginning of the next command, even if the output is not shown. This is demonstrated by the echo line before the if statement, which is often cut off. But even for lines that aren't cut off, most of them will be skipped for no apparent reason.

Was it helpful?

Solution

ffmpeg reads from stdin, thereby consuming input meant for while read. Just redirect stdin for ffmpeg by adding < /dev/null

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top