Skip to content

Instantly share code, notes, and snippets.

@gotev
Created November 19, 2022 10:10
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 gotev/c69d41b29393b15817e88dfbb88a6887 to your computer and use it in GitHub Desktop.
Save gotev/c69d41b29393b15817e88dfbb88a6887 to your computer and use it in GitHub Desktop.
Re-Encode Mp4 Video Files using H.264 and AAC audio, optimized for streaming
#!/bin/bash -e
# Re-Encode Mp4 Video Files using H.264 and AAC audio, optimized for streaming
# Credits: https://superuser.com/a/1551095
# -c:v libx264 selects the video encoder libx264, which is a H.264 encoder.
# -preset slow selects the slow x264 encoding preset. Default preset is medium. Use the slowest preset that you have patience for.
# -crf 20 selects a CRF value of 20 which will result in a high quality output. Default value is 23. A lower value is a higher quality. Use the highest value that gives an acceptable quality.
# -c:a aac selects the audio encoder aac, which is the built-in FFmpeg AAC encoder.
# -b:a 160k encodes the audio with a bitrate of 160k.
# -vf format=yuv420p chooses YUV 4:2:0 chroma-subsampling which is recommended for H.264 compatibility.
# -movflags +faststart is an option for MP4 output that move some data to the beginning of the file after encoding is finished. This allows the video to begin playing faster if it is watched via progressive download playback.
echo "Re encode mp4 files in standard mp4 h264 with aac audio, optimized for fast playback when streaming"
rm -rf reencoded
mkdir reencoded
for video in *.mp4
do
echo
echo "Processing $video"
echo
ffmpeg -i "$video" -c:v libx264 -preset slow -crf 20 -c:a aac -b:a 160k -vf format=yuv420p -movflags +faststart "reencoded/$video"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment