changed order of operations for moving files to be safer overall

This commit is contained in:
Dan Snyder 2025-02-20 19:25:00 -05:00
parent e995a59662
commit dd8b68d722

View File

@ -72,29 +72,37 @@ encodeFile() {
tmpfile="$infile.bak"
ffmpeg -nostdin -hide_banner -i "$infile" -c:v hevc_nvenc -c:a copy -c:s copy -map 0 -vtag hvc1 output.mkv
if [ $? -ne 0 ]; then
echo "FFMPEG re-encode failed: $infile"
return 1
fi
mv "$infile" "$tmpfile"
if [ $? -ne 0 ]; then
echo "Failed to make backup file: $infile"
rm output.mkv
return 2
fi
ffmpeg -nostdin -hide_banner -i "$tmpfile" -c:v hevc_nvenc -c:a copy -c:s copy -map 0 -vtag hvc1 "$outfile"
mv output.mkv "$outfile"
if [ $? -ne 0 ]; then
echo "FFMPEG re-encode failed: $infile"
echo "Restoring from backup"
echo "Failed final move to: $outfile"
mv -f "$tmpfile" "$infile"
return 1
fi
rm output.mkv
return 2
fi
echo "Output file: $outfile"
rm -f "$tmpfile"
oldsize=$( echo "$filesize" | formatSize )
oldbitr=$( echo "$bitrate" | formatRate )
newsize=$( fileSize "$outfile" | formatSize )
newbitr=$( getBitrate "$outfile" | formatRate )
rm "$tmpfile"
oldsize=$( formatSize $filesize )
oldbitr=$( formatRate $bitrate )
newsize=$( formatSize $( fileSize "$outfile" ) )
newbitr=$( formatRate $( getBitrate "$outfile" ) )
echo "Converted $infile:" >> summary.txt
echo "\t$oldsize ($oldbitr) => $newsize ($newbitr)\n" >> summary.txt
return 0
@ -183,7 +191,7 @@ if [ "$mode" != "sim" ]; then
cat simulation.txt | while read line; do
encodeFile "$line"
if [ $? -ne 0 ]; then
if [ $? -eq 2 ]; then
exit 2
fi
done