0

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
0

Thanks to Gordon Davisson I managed to solve the problem. Here is the complete working script if someone happens to stumple upon this issue in the future.

#!/bin/bash -e
find /videos/. -type f -exec sh -c 'h=$(date -d @$(stat -c %Y "$1") +%-H); [ "$h" -ge 00 ] && [ "$h" -lt 12 ]' sh {} \; -print | while IFS= read -r i;
do 
    ffmpeg -y -i "$i" -vcodec libx264 -crf 27 -preset veryfast -movflags +faststart -c:a copy -threads 14 /output/"$(basename "$i" .ts)".mp4 </dev/null
    rm -f -- "$i"
done
  • A couple more recommendations occur to me: I'd move the time check into the while loop, so you're only using a single shell -- that is, have find list all files, then check the date in the loop & only run ffmpeg if it's in the right range. Also, it'd be best to make the rm conditional on ffmpeg succeeding. So it should be something like `while ... do h=$(...); [ "$h" -ge 00 ] && [ "$h" -lt 12 ] && ffmpeg ... && rm ...; done – Gordon Davisson Mar 12 at 2:03
  • I agree with your comment Gordon. I have another issue with files modified during hour 23. For example files modified 23:45 will not be included in the list. How can I solve this? – Kaah Mar 26 at 18:43
  • I solved it by using the '-le' operator listed here: tldp.org/LDP/abs/html/comparison-ops.html – Kaah Mar 26 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.