Tuesday, March 25, 2025

Scripts For Converting Various Video Formats To Mp4 Using ffmpeg

 My scripts for converting various video formats to mp4 using ffmpeg, which is amazing!

Used my macbook with the M4 Pro chip in zsh and it flew through all these.


<pre>

#Converts all *.mov files to mp4

for file in *.mov; do

    ffmpeg -i "$file" -c:v libx264 -crf 23 -c:a aac -b:a 128k -movflags +faststart "${file%.mov}.mp4"

done


for file in *.MOV; do

    ffmpeg -i "$file" -c:v libx264 -crf 23 -c:a aac -b:a 128k -movflags +faststart "${file%.MOV}.mp4"

done


for file in *.avi; do

     ls -la "$file"

    ffmpeg -i "$file" -c:v libx264  -preset veryslow -crf 23 -c:a aac -b:a 128k -movflags +faststart "${file%.avi}.mp4"

done


for file in *.m4v; do

   ffprobe -v error -show_streams -select_streams v:0  "$file" | grep codec_name

done


for file in *.mpg; do

     ls -la "$file"

    ffmpeg -i "$file" -c:v libx264  -preset veryslow -crf 23 -c:a aac -b:a 128k -movflags +faststart "${file%.mpg}.mp4"

done


#this reduces the size of an mp4

ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset veryslow -c:a copy output.mp4

for file in *.mp4; do

   ffmpeg -i "$file" -c:v libx264 -crf 28 -preset veryslow -c:a copy "$file"-new.mp4

done

for file in *.m4v; do

    ls -la "$file"

    ffmpeg -i "$file" -c:v libx264  -preset veryslow -crf 23 -c:a aac -b:a 128k -movflags +faststart "${file%.m4v}.mp4"

done

# this re-encodes the mp4, but makes it smaller

for file in *.mp4; do

   ffmpeg -i "$file" -c:v libx264 -crf 28 -preset veryslow -c:a copy "$file"-new.mp4

done


#With certain processes I would define a variable and use it in scripts

#The ffmpeg command below shrinks the size of the file without substantially altering quality

export video="1997-02-12-Baby.m4v"

export video='1990-06-29-Mike.mp4'

ffmpeg -i "$video" -c:v libx264 -crf 28 -preset veryslow -c:a copy "$video"-new.mp4


</pre>