-
-
Save anthonybaldwin/f784bdb0f51e47aedad0a89667dbafc3 to your computer and use it in GitHub Desktop.
Convert any video (i.e.: AVI, MOV, MP4) to MP4 or WEBM with FFmpeg. Has option to export MP4 with YUV planar color space so it works with QuickTime and Safari.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Version 1.0 2016-06-03 https://gist.github.com/kus/318f21b840df0b7a6377563ce717c184 | |
# MIT license | |
# Prerequisites: | |
# Homebrew: http://brew.sh/ | |
# Install FFmpeg via Homebrew: brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvpx --with-theora --with-libogg --with-libvorbis --with-opus --with-x265 | |
# Make the script executable: sudo chmod u+x convert-video.sh | |
# Run: ./convert-video.sh | |
# Output directory | |
OUTPUT_DIR="output/" | |
# The video extension you want to target | |
EXTENSION=".mp4" | |
TRANSPARENT=false | |
# Output files. Change from true/false | |
# MP4 | |
OUTPUT_MP4=false | |
# MP4 with YUV planar color space with 4:2:0 chroma subsampling (QuickTime/Safari support) | |
OUTPUT_MP4_YUV=false | |
# WebM | |
OUTPUT_WEBM=true | |
# Delete output directory | |
DELETE_OUTPUT_DIR=false | |
# Work out current directory | |
WORKING_DIR=$(cd "${0%/*}" && echo "${PWD}/") | |
# Delete output directory | |
if $DELETE_OUTPUT_DIR; then | |
rm -rf "${WORKING_DIR}${OUTPUT_DIR}" | |
fi | |
# Create output directory if it doesn't exist | |
mkdir -p "${WORKING_DIR}${OUTPUT_DIR}" | |
# H264 https://trac.ffmpeg.org/wiki/Encode/H.264 | |
# -preset (Preset) | |
# A preset is a collection of options that will provide a certain encoding speed to compression ratio. | |
# ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow | |
# -crf (Constant Rate Factor) | |
# Quality 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28 | |
# Bitrates | |
# -b:v 500k -maxrate 500k -bufsize 1000k -b:a 128k | |
# Force AAC audio | |
# -codec:a libfdk_aac | |
# Encoding for dumb players / Apple Quicktime https://trac.ffmpeg.org/wiki/Encode/H.264 | |
# You may need to use -pix_fmt yuv420p for your output to work in QuickTime and most other players. These players only supports the YUV planar color space with 4:2:0 chroma subsampling for H.264 video. Otherwise, depending on your source, ffmpeg may output to a pixel format that may be incompatible with these players. | |
# -pix_fmt yuv420p | |
# Divisible by 2 | |
# You may need to make the video divisible by 2 when using certain options, if you do you can use this: | |
# -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" | |
# WebM https://trac.ffmpeg.org/wiki/Encode/VP8 | |
# -crf (Constant Rate Factor) | |
# By default the CRF value can be from 4–63, and 10 is a good starting point. Lower values mean better quality. | |
# You may need to use -pix_fmt yuva420p for your output to work in QuickTime and most other players. These players only supports the YUV planar color space with 4:2:0 chroma subsampling for H.264 video. Otherwise, depending on your source, ffmpeg may output to a pixel format that may be incompatible with these players. | |
# Divisible by 2 | |
# You may need to make the video divisible by 2 when using certain options, if you do you can use this: | |
# -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" | |
FILES=(*${EXTENSION}) && \ | |
for INDEX in "${!FILES[@]}"; do | |
FOLDER_PATH=${FILES[INDEX]} | |
FILENAME="${FOLDER_PATH##*/}" | |
FILENAME_NO_EXTENSION="${FILENAME%.*}" | |
if test "$FILENAME_NO_EXTENSION" != "*"; then | |
echo "Converting ${FILENAME_NO_EXTENSION}..." | |
if $OUTPUT_MP4; then | |
ffmpeg \ | |
-i "${WORKING_DIR}${FILENAME}" \ | |
-vcodec h264 \ | |
-acodec aac \ | |
-preset slow \ | |
-crf 23 \ | |
-b:v 2500k -maxrate 500k -bufsize 1000k -b:a 128k \ | |
-y \ | |
"${WORKING_DIR}${OUTPUT_DIR}${FILENAME_NO_EXTENSION}.mp4" | |
fi | |
if $OUTPUT_MP4_YUV; then | |
ffmpeg \ | |
-i "${WORKING_DIR}${FILENAME}" \ | |
-vcodec h264 \ | |
-acodec aac \ | |
-preset slow \ | |
-crf 23 \ | |
-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" \ | |
-pix_fmt yuv420p \ | |
-b:v 2500k -maxrate 500k -bufsize 1000k -b:a 128k \ | |
-y \ | |
"${WORKING_DIR}${OUTPUT_DIR}${FILENAME_NO_EXTENSION}_yuv.mp4" | |
fi | |
if $OUTPUT_WEBM; then | |
if $TRANSPARENT; then | |
AUTO_ALT_REF=0 | |
else | |
AUTO_ALT_REF=1 | |
fi | |
ffmpeg \ | |
-i "${WORKING_DIR}${FILENAME}" \ | |
-vcodec libvpx \ | |
-acodec libvorbis \ | |
-preset slow \ | |
-auto-alt-ref ${AUTO_ALT_REF} \ | |
-crf 30 \ | |
-b:v 2500k -maxrate 500k -bufsize 1000k -b:a 128k \ | |
-y \ | |
"${WORKING_DIR}${OUTPUT_DIR}${FILENAME_NO_EXTENSION}.webm" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment