0

I currently have a simple script that I run to convert videos using the Handbrake CLI. What I would love is to modify the script so that if the files convert successfully the original file is deleted.

Here is the script as it stands:

#!/bin/sh

IN=$1
OUT=$2

cd "$IN"
for InputItem in *;do
  /path/to/HandBrakeCLI -i "$InputItem" -o "$OUT/${InputItem}.mp4" -e x264  -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0
done

Any ideas?

Mitch Malone
  • 882
  • 6
  • 17

2 Answers2

0

I've done something similar in C#, in the I end used ffprobe to compare the duration of the original vs the converted file, if they match then rm the source file, or mv and overwrite the original version with the newly converted file.

  • could you include how to do that comparison? – keen Feb 01 '23 at 19:37
  • see https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net to start FFMPEG and capture the output... function string GetInfo(string FFMPEGOUTPUT) { return FFMPEGOUTPUT.Substring(Data.ToString().IndexOf("Duration:") + "Duration:".Length, 12); } eg: FFMPEG returns string Duration in the output, Grab substring from inputfile, output file and compare Then you know you've at least converted the entire file bec presumably transcoding will result in dif file size... –  Feb 02 '23 at 22:31
  • I mean you say: "your answer just says `hey, I did that once`" without actually answering the question. expanding your answer to include _how_ you did it would be make it a more useful answer. – keen Feb 22 '23 at 18:34
-1

You can check the exit status of your program. Usually successful exit returns zero.

To do this, inside the loop, after the run, test for exit status:

if [ $? -eq 0 ]
then
  rm "$InputItem"
fi
hesham_EE
  • 1,125
  • 13
  • 24
  • can you confirm that handbrake would exit non-zero in this event? this feels risky unless you know for certain that a zero exit means the task you wanted was completed... – keen Feb 01 '23 at 19:37