Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active March 15, 2024 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/488e7e4bc781a4b34385af887e9e8a48 to your computer and use it in GitHub Desktop.
Save james2doyle/488e7e4bc781a4b34385af887e9e8a48 to your computer and use it in GitHub Desktop.
Downscale a folder of mp4 files using ffmpeg. This loop takes an input video file, scales it to a height of 720 pixels while maintaining the aspect ratio, encodes the video using H.264 and the audio using AAC, and saves the output as an MP4 file with the same base name as the input file, but with "0-" prepended to it.
#!/usr/bin/env bash
# The provided ffmpeg command performs several operations:
#`-vf "scale=-1:720,setsar=1"`: The `-vf` option stands for "video filter" and allows you to apply various video processing operations. In this case, two filters are applied:
# - `scale=-1:720`: This filter scales the video vertically to a height of 720 pixels, while maintaining the original aspect ratio. The `-1` value for the width means that the width will be calculated automatically based on the new height.
# - `setsar=1`: This filter sets the sample aspect ratio (SAR) to 1, which ensures that the video is displayed with square pixels.
#`-c:v libx264`: This option specifies the video codec to be used for the output file. In this case, it's the x264 codec, which is a popular and widely-used H.264 video codec.
#`-crf 20`: The Constant Rate Factor (CRF) is a quality setting for the x264 codec. A lower CRF value (e.g., 20) results in higher quality and larger file size, while a higher CRF value (e.g., 28) results in lower quality and smaller file size.
#`-c:a aac`: This option specifies the audio codec to be used for the output file. In this case, it's the Advanced Audio Coding (AAC) codec.
#`-ar 44100`: This sets the audio sampling rate to 44.1 kHz, which is a common sample rate for audio.
#`-b:a 96k`: This sets the maximum bitrate for the audio stream to 96 kilobits per second.
#`-threads 8`: This option tells FFmpeg to use 8 threads for the encoding process, which can potentially speed up the processing.
# The final output file will have a ".mp4" extension and will be named "0-$FILE%.mp4", where "$FILE" represents the name of the input file without any extensions.
for FILE in *.mp4
do
echo "file - $FILE"
ffmpeg -i "$FILE" -vf "scale=-1:720,setsar=1" -c:v libx264 -crf 20 -c:a aac -ar 44100 -b:a 96k -threads 8 "0-${FILE%.*}.mp4";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment