Skip to main content
edited tags
Link
Jeff Schaller
  • 68.9k
  • 35
  • 122
  • 267
Source Link
Kaah
  • 121
  • 4

Creating a for loop with find -exec and while

I am trying to make this script that loops through video files created between hours 00 and 12, convert them with ffmpeg and then remove them.

The script works in terms of finding the files and starting ffpmeg but it seems that it continues to "send" characters from the find -exec after that ffmpeg has started on the first conversion and it eventually breaks ffmpeg and stop the conversion. How can I modify the script so this does not happen?

The current script

!/bin/bash -e
find /videos/. -type f -print0 -exec sh -c 'h=$(date -d @$(stat -c %Y "$1") +%-H); [ "$h" -ge 00 ] && [ "$h" -lt 12 ]'   sh {} \;|while read -d $'\0' i;
do 
    ffmpeg -y -i "$i" -vcodec libx264 -crf 27 -preset veryfast -movflags +faststart -c:a copy -threads 14 /output/"$(basename "$i" .ts)".mp4
    rm -f -- "$i"
done