My NAS has a lot of old privat video files and I want to convert them to the new h265-codec to save a lot of space. First thing to say is that you need a lot of CPU Power to process the video transcoding. Plan is to create a script that search the folder structure and then convert the file, remove the old (mov or mp4 file) and replace it with the new h265 encoded file.
After some research I got to following:
with help of Marcel in this post and Thomas Böhm
CPU rendering
find * -type f \( -iname \*.mp4 -o -iname \*.mov \) -exec ffmpeg -i {} -vcodec libx265 -crf 18 -vtag hvc1 temp_{} \; -exec mv temp_{} {} \;
If you just convert one file you use following command:
ffmpeg -i <path_to_orignal_file> -c:v libx265 -vtag hvc1 -crf 18 <path_to_output_file>
You can change the setting -crf to optimise you output files bitrate
If you want to know more about it visit Werner Robitza Homepage LINK
Apple T2 Chip – faster transcoding
If you use an Apple Mac Device with an T2 Chip (most OS devices after 2018) you can speed up the conversation a lot by using the hevc_videotoolbox of ffmpeg. This will increase the speed more than 30 times, compared to CPU rendering. BUT the quality is getting really bad. I would recommend to use the classical CPU rendering.
ffmpeg -i <path_to_orignal_file> -vcodec hevc_videotoolbox -vtag hvc1 -crf 18 <path_to_output_file>
After some testing and playing around I created following script. The logging is in englisch and german mixed, sorry for that.
You need to install ffmpeg and exiftool (this will help copy the metadata of the original video file to the new created one, so you do not lose the creation date). On a Mac you can use Homebrew to install the packages.
First install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
then ffmpeg:
brew install ffmpeg
and now exiftool:
brew install exiftool
Now you can create a new shell script and copy the content into your empty file. Move the file to the folder which you want to transcode (take care the it will automatically do it recursive). Then you have to change the last line in the script to the folder path you have stored the script. Now you can start the transcoding with
chmod + x <your-script-name>
#and then
./<your-script-name>
#!/bin/zsh
# this script can help you to convert files in folders and subfolder to h265 files (using CPU rendering, you can change the ffmpeg parameters if you need)
# copy the script to the folder you want to convert files, and change the path in the last lines of the script
# you need ti have ffmpeg and exiftool installed. On the mac you can use Homebrew
mkdir ~/Documents/log
machwas() {
wo_bin_ich=$( pwd )
echo $date "bearbeite Ordner ${wo_bin_ich}/ -------" >> ~/Documents/log/H265_transcoding.log
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *; do
# fuer alle Ordner
if [ -d "$f" ]; then
echo $date "habe Ordner $f erkannt" >> ~/Documents/log/H265_transcoding.log
(cd -- "$f" && machwas)
fi
if [ $(find $f -type f -maxdepth 0 -iname "*.mp4" -o -iname "*.mov"| wc -l) -gt 0 ]
then
echo $date $f 'is mov or mp4' >> ~/Documents/log/H265_transcoding.log
if mdls -name kMDItemCodecs $f | grep -q "HEVC"
then
echo $date $f 'is HEVC' >> ~/Documents/log/H265_transcoding.log
else
echo $date $f 'is not HEVC, starting to prozess the files ATTENTION' >> ~/Documents/log/H265_transcoding.log
# echo $f
name=$(basename "$f")
# echo $name
ffmpeg -i $f -vcodec libx265 -crf 18 -vtag hvc1 /tmp/$name >> ~/Documents/log/H265_transcoding.log
exiftool -tagsFromFile $f -api largefilesupport=1 -extractEmbedded -all:all -FileModifyDate -overwrite_original /tmp/$name >> ~/Documents/log/H265_transcoding.log
mv /tmp/$name $f
fi
else
echo $date $f ' this is not an video file of the type mp4 or mov' >> ~/Documents/log/H265_transcoding.log
fi
done
}
IFS=$SAVEIFS
# Aufruf des Ganzen
cd /Users/admin/Movies
machwas
Maybe I will get time in the next weeks to change the language to english and optimize the workflow.