
Made a ffmpeg video converter to H.265 script to save space on my video files
Hello, I would love some feedback.
I have made a Bash script using to convert a bunch of .mp4 files to a newer, less space hungry codec (H.265) without a drop in quality.
It only scan for .mp4 files but can be changed quite easily.
After converting, it append "_cc" to the end of the filename, it will also not convert files who already have that substring.
It will delete the original at the end, but can be changed also if needed, then give you info about the total space saved and how long the script was running
https://github.com/PassPhoenix/ffmpeg_converter_H265/blob/main/ffmpeg_h265_converter_mp4.sh
Is there a better way to go about the wall of "echo -n" I did? The code in general?
It's a very simple script and I am learning
Or here for the code:
#! /usr/bin/bash
input_format="mp4"
datasaved=0
SECONDS=0
number_files_converted=0
for file in *."$input_format"; do
base_name=$(basename "$file" .$input_format)
output_file="${base_name}_cc.${input_format}"
if [[ $base_name == *"_cc"* ]]; then
continue
fi
size_vid=$(stat --format "%s" "$file")
echo "Converting file: $file"
ffmpeg -hide_banner -loglevel error -i "$file" -c:v libx265 -x265-params log-level=none -crf 28 -c:a copy "$output_file"
size_vid_after=$(stat --format "%s" "$output_file")
echo -n "Converted $file ("
echo -n "$size_vid"| numfmt --to iec
echo -n ") to $output_file ("
echo -n "$size_vid_after" | numfmt --to iec
echo -n ") Reduced by -"
echo -n $((size_vid - size_vid_after)) | numfmt --to iec
echo "."
((datasaved+= size_vid - size_vid_after))
echo -n "Size saved so far: "
echo $datasaved | numfmt --to iec
printf "\n"
rm "$file"
((number_files_converted++))
done
echo -n "Total saved is "
echo $datasaved | numfmt --to iec
duration=$SECONDS
echo "$((duration / 60)) minutes and $((duration % 60)) seconds elapsed for $number_files_converted files converted."