Skip to content

Instantly share code, notes, and snippets.

@teknikqa
Last active May 3, 2018 07:25
Show Gist options
  • Save teknikqa/9354f65f8e9608bbef1c3a68cce1244f to your computer and use it in GitHub Desktop.
Save teknikqa/9354f65f8e9608bbef1c3a68cce1244f to your computer and use it in GitHub Desktop.
Convert AVI videos to WEBM & MP4 videos for use in webpage backgrounds.

MP4

Pass 1

ffmpeg -i input.avi -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:720 -threads 0 -pass 1 -an -f mp4 /dev/null

Pass 2

ffmpeg -i input.avi -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:720 -threads 0 -pass 2 -an -f mp4 output.mp4

WEBM

Pass 1

ffmpeg -i input.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:720 -an -pass 1 -f webm /dev/null

Pass 2

ffmpeg -i input.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:720 -an -pass 2 -f webm output.webm

Reference

#!/bin/bash
#
# Script to convert a video to WEBM and MP4 formats for the website.
#
# This script will convert a video file into WEBM and MP4 formats. In the
# process, it will remove sound and optimize the size of the output video so as
# to be able to use as Hero Videos on the website. The resulting output file
# will be less than 10 MB, often within 3-7 MB.
#
# Usage: convert_video.sh input [output]
# Requires: FFMPEG with libvpx. See https://trac.ffmpeg.org/wiki/Encode/VP8
# Author: Nick Mathew
if [ -z "${1}" ]
then
printf "Please enter a filename to process. Usage: convert_video.sh input [output]\\n"
exit
else
INPUT="${1}"
fi
DATE=$(date '+%Y%m%d-%H%M')
if [ -z "${2}" ]
then
OUTPUT="${INPUT:0:${#INPUT} - 4}-${DATE}"
else
OUTPUT="${2}-${DATE}"
fi
# Convert into MP4.
# See https://www.virag.si/2012/01/web-video-encoding-tutorial-with-ffmpeg-0-9/
ffmpeg -y -i "${INPUT}" -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:720 -threads 0 -pass 1 -an -f mp4 /dev/null
ffmpeg -y -i "${INPUT}" -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:720 -threads 0 -pass 2 -an -f mp4 "${OUTPUT}".mp4
# Convert into WEBM.
# See https://www.virag.si/2012/01/webm-web-video-encoding-tutorial-with-ffmpeg-0-9/
ffmpeg -y -i "${INPUT}" -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:720 -an -pass 1 -f webm /dev/null
ffmpeg -y -i "${INPUT}" -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:720 -an -pass 2 -f webm "${OUTPUT}".webm
# Capture a screenshot from 10 seconds into the video and save it as 1024x800 px image.
ffmpeg -ss 10 -i "${INPUT}" -vframes 1 -s 1024x800 "${OUTPUT}".png
printf "Completed converting the files.\\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment