-1

I want to make sure the transcoding was successful before deleting the original file. I'm quite new to for loops in BASH shell scripting.

for i in *.mp4; do 
  ffmpeg -i "$i" -c:v libx265 -crf 39 -c:a aac -b:a 128k -y new-"$i";
done
B. Shea
  • 829
  • 14
  • 29
Ace
  • 109
  • 2
  • 2
    "verify the new file against the original file" -- what do you mean? Lossy encoding means that they won't be identical even if everything worked right. If what you want is a tool to compare two videos against each other and try to decide if they're within a certain threshold of similarity to each other... that's a pretty complicated tool, bash isn't the right language to write it in, and even if it already exists, tool recommendation questions are categorically off-topic. – Charles Duffy Sep 04 '22 at 20:17
  • Anyhow -- we can definitely help you with the bash for-loop parts of your problem, but you need to be responsible for figuring out which tools you want that loop to invoke. Can you narrow in on a specific command you want the loop to run but are having trouble with? – Charles Duffy Sep 04 '22 at 20:19
  • (on the other hand, if what you want to do is delete the original file _if ffmpeg reports that the conversion was successful_ without needing a separate check phase, that's a lot easier than trying to do an explicit verification pass). – Charles Duffy Sep 04 '22 at 20:31
  • Ok I just want to delete the original file after it finished by ffmpeg in the for loop. I'm not sure how add an rm using a variable of the original file which was the source for transcoding. I'm ok with the following `delete the original file if ffmpeg reports that the conversion was successful` The check was a nice to have but not a must. – Ace Sep 04 '22 at 21:48
  • 1
    After the `new-"$i"` add a space and `&& test -e new-"$i" && rm "$f" ; done` – Jetchisel Sep 05 '22 at 04:34
  • @ace You should edit your question. If you simply wish to test the produced file in some way after ffmpeg exits, see https://stackoverflow.com/a/34088828/503621 or you could use FFPROBE. To check output, just add some lines to your script under the ffmpeg statement. You might read this too: https://ffmpeg.org/ffprobe.html – B. Shea Sep 05 '22 at 13:59
  • I did edited my question, I removed it. What I wanted is to make sure the transcoding was successful before deleting the original file. @B.Shea – Ace Sep 05 '22 at 14:25
  • @ace Please see my comment for answer in that case.. ALSO: Paraphrased: "I want to make sure the transcoding was successful before deleting the original file." < Worded clearly. Edit question and add that sentence. :-) – B. Shea Sep 05 '22 at 14:37
  • Yeah that's my fault. I thought you guys understood that testing the file meant that the transcode completed successfully. – Ace Sep 05 '22 at 14:41
  • @ace NP. Live and learn. Unless you state it explicitly, we have to assume in order to answer questions like these. (Even if we are 99% sure on what you are asking) Stack Q&A frowns on assuming in order to answer a question. State all new question clearly and consisely. You have plenty of room to elaborate. Stack sites want good questions so they can help others and not just you. Think Google/et al. – B. Shea Sep 05 '22 at 14:45
  • https://stackoverflow.com/a/34088828/503621 https://stackoverflow.com/q/38477974/503621 Linking to question. See these for help on scanning "integrity" of video files. – B. Shea Sep 05 '22 at 14:56
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 06 '22 at 07:22

1 Answers1

0
ffprobe -v error "$filename"

or

ffmpeg -v error -i "$filename" -f null -

will check a media file.

BETTER:

You could just check the ffmpeg exit code status when it completes each file. I do not really see a point to quick scan the entire new file - it either worked or it didn't per ffmeg output and exit status when completed:

for i in *.mp4; do 
  ffmpeg -i "$i" -c:v libx265 -crf 39 -c:a aac -b:a 128k -y new-"$i";
  if [[ $? == 0 ]]; then
    printf "FILE OKAY  -\t %s \n" $i
  else
    printf "FILE ERROR -\t %s \n" $i
  fi
done

In BASH, $? holds last executed command status ("exit status"). Normally, if this isn't '0', you had an error running last command. This would seem to be best bet if I understand question correctly.

You could also do it after all the files are produced with something like:

for i in ./*.mp4; do

 ffmpeg -v error -i ${i} -f null - 2>/dev/null;
 if [[ $? == 0 ]]; then
   printf "FILE OKAY  -\t %s \n" $i
     else
   printf "FILE ERROR -\t %s \n" $i
 fi
done

Output:

FILE OKAY  -     ./access.mp4
FILE OKAY  -     ./another.mp4
FILE ERROR -     ./bad.mp4
B. Shea
  • 829
  • 14
  • 29