FFMPEG/Libav

Vicente González Ruiz

September 12, 2016

Contents

1 Introduction
 1.1 FFMPEG vs Libav
 1.2 Free sofware and patents
2 Installation
 2.1 FFMPEG
  2.1.1 Ubuntu
  2.1.2 Arch Linux
 2.2 Libav
  2.2.1 Ubuntu
3 Tools and help
 3.1 FFMPEG/Libav command line tools
 3.2 manual
 3.3 Built-in help
 3.4 Synopsis
4 File inspection
5 Capturing
 5.1 Recognized devices
 5.2 Sound rcording (sound) from ALSA
 5.3 Video recording from a V4L device
 5.4 Video recording from the X11
6 Converting
 6.1 Supported formats and codecs
 6.2 H.264 (x264)
  6.2.1 Presets and Tunes
  6.2.2 Example
  6.2.3 Profiles
  6.2.4 Quality control
  6.2.5 Bit-rate control
  6.2.6 Lossless encoding
 6.3 HEVC
 6.4 3D
7 Editing
 7.1 Extracting and inserting images
 7.2 Creating two videos in parallel
 7.3 Copy and paste
8 Transcoding
 8.1 Bit-rate
 8.2 Frame-rate
9 Streaming
 9.1 One-to-one unicast
 9.2 Multicast
 9.3 One-to-many unicast

Chapter 1
Introduction

1.1 FFMPEG vs Libav

1.2 Free sofware and patents

Chapter 2
Installation

2.1 FFMPEG

2.1.1 Ubuntu

2.1.2 Arch Linux

2.2 Libav

2.2.1 Ubuntu

sudo apt-get install x264           # H.264  
sudo apt-get install libav-tools    # avconv

Chapter 3
Tools and help

3.1 FFMPEG/Libav command line tools

  1. ffmpeg/avconv: encodes and transcodes.
  2. ffplay/avplay: playbacks a video.
  3. ffprobe/avprobe: gathers information from multimedia streams.
  4. ffserver/avserver: a streaming server for both audio and video

3.2 manual

3.3 Built-in help

ffmpeg -help  
avconv -help

3.4 Synopsis

ffmpeg/aconv [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...

Chapter 4
File inspection

wget http://www.hpca.ual.es/~vruiz/videos/coastguard_352x288x30x420x300.avi  
 
ffmpeg -i /tmp/coastguard_352x288x30x420x300.avi  
# or ...  
ffprobe /tmp/coastguard_352x288x30x420x300.avi

Chapter 5
Capturing

5.1 Recognized devices

ffmpeg -devices  
avconv -formats

5.2 Sound rcording (sound) from ALSA

# Record 30 seconds, stereo, CD quality, from the ALSA mic  
ffmpeg -f alsa -ac 2 -ar 44100 -i default -t 30 out.wav  
ffmpeg -f alsa -ac 2 -ar 44100 -i hw:0 -t 30 out.wav  
avconv -f alsa -ac 2 -ar 44100 -i hw:0 -t 30 out.wav

5.3 Video recording from a V4L device

ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 /tmp/1.avi # (/dev/video0 = WebCam)  
avconv -f video4linux2 -s 640x480 -r 30 -i /dev/video0 /tmp/1.avi

5.4 Video recording from the X11

# Grab CIF region from origin:  
ffmpeg -f x11grab -framerate 25 -video_size cif -i :0.0 -f mpegts - | ffplay -  
#                                                   ^ ^  
#                                                   | |  
#                                                   | +-----+  
#                                                   |       |  
#                                                  :server.display  
 
# Grab from coordinates (10, 20):  
ffmpeg -f x11grab -show_region 1 -framerate 25 -video_size cif -i :0.0+10,20 -f mpegts - | ffplay -  
avconv -f x11grab -show_region 1 -framerate 25 -video_size cif -i :0.0+10,20 -f mpegts - | avplay -  
 
# Grab following the mouse (centered in the captured window):  
ffmpeg -f x11grab -show_region 1 -follow_mouse centered -framerate 25 -video_size cif -i :0.0 -f mpegts - | ffplay -  
avconv -f x11grab -show_region 1 -follow_mouse centered -framerate 25 -video_size cif -i :0.0 -f mpegts - | avplay -  
 
# Capture the full screen, with audio, and produce a file:  
ffmpeg -framerate 25 -f x11grab -i :0.0 -f alsa -ac 2 -i default output.flv

Chapter 6
Converting

6.1 Supported formats and codecs

ffmpeg -formats # List formats (containers)  
ffmpeg -codecs  # List codecs  (algorithms)

6.2 H.264 (x264)

6.2.1 Presets and Tunes

6.2.2 Example

ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -c:v libx264 -tune zerolatency -preset medium -b:v 800k -f mpegts - | ffplay -  
 
avconv -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -c:v libx264 -tune zerolatency -preset medium -b:v 800k -f mpegts - | avplay -

6.2.3 Profiles

# Highest compatibility (-profile:v baseline -level 3.0)  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v high -level:v 4.0 -b:v 800k -f mpegts - | ffplay -  
 
# Full compatibility (-profile:v baseline -level 30  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v baseline -level:v 30 -b:v 800k -f mpegts - | ffplay -  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v baseline -level:v 3.0 -b:v 800k -f mpegts - | ffplay -  
avconv -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v baseline -level:v 30 -b:v 800k -f mpegts - | avplay -  
 
# Apple Devices from 2010 (-profile:v main -level 3.1)  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v high -level:v 3.1 -b:v 800k -f mpegts - | ffplay -  
avconv -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v high -level:v 31 -b:v 800k -f mpegts - | avplay -  
 
# Apple Ipad2 and AppleTV 3 (-profile:v high -level 4.1)  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v baseline -level:v 41 -b:v 800k -f mpegts - | ffplay -  
avconv -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -pix_fmt yuv420p -c:v libx264 -profile:v baseline -level:v 41 -b:v 800k -f mpegts - | avplay -

6.2.4 Quality control

6.2.5 Bit-rate control

6.2.6 Lossless encoding

  1. Fastest encoding:
    avconv -i input -c:v libx264 -preset superfast -qp 0 -c:a copy out.mkv

  2. Smallest filesize:
    avconv -i input -c:v libx264 -preset veryslow -qp 0 -c:a copy out.mkv

6.3 HEVC

# H.264 lossless:  
wget http://www.hpca.ual.es/~vruiz/videos/coastguard_352x288x30x420x300.avi  
 
# Lossy encode:  
ffmpeg -i coastguard_352x288x30x420x300.avi -c:v hevc coastguard_352x288x30x420x300.mp4  
 
# Get info:  
ffprobe coastguard_352x288x30x420x300.mp4  
 
# Play it:  
ffprobe coastguard_352x288x30x420x300.mp4

6.4 3D

# Download a SBS 3D video:  
youtube-dl https://www.youtube.com/watch?v=YWWbZLl2evE  
 
# Change the name:  
mv Hang Gliding With The GoPro - 3D Side by Side (SBS)-YWWbZLl2evE.mp4 sbs.mp4  
 
# Convert from SBS to "Red cyan gray/monochrome" anaglyph:  
ffmpeg -i sbs.mp4 -vf stereo3d=sbs2l:arcg -acodec copy anaglyph.mp4

Chapter 7
Editing

7.1 Extracting and inserting images

wget http://www.hpca.ual.es/~vruiz/videos/coastguard_352x288x30x420x300.avi  
 
# Extracting the images of a (color) video  
ffmpeg -i coastguard_352x288x30x420x300.avi coastguard_%3d.png  
avconv -i coastguard_352x288x30x420x300.avi coastguard_%3d.png  
animate *.png  
 
# Creating a video with a collection of images  
ffmpeg -i coastguard_%3d.png coastguard.avi  
avconv -i coastguard_%3d.png coastguard.avi  
mplayer coastguard.avi

7.2 Creating two videos in parallel

# Creating two videos (with two different formats) in parallel  
ffmpeg -i coastguard_352x288x30x420x300.avi -map 0 1.mkv -map 0 2.mp4  
ffmpeg -i 1.mkv  
ffmpeg -i 2.mp4  
 
avconv -i coastguard_352x288x30x420x300.avi -map 0 1.mkv -map 0 2.mp4  
avconv -i 1.mkv  
avconv -i 2.mp4

7.3 Copy and paste

wget http://www.hpca.ual.es/~vruiz/videos/mobile_352x288x30x420x300.avi  
 
ffmpeg -i coastguard_352x288x30x420x300.avi  
ffmpeg -i mobile_352x288x30x420x300.avi  
 
# Concatenate two videos  
ffmpeg -i "concat:coastguard_352x288x30x420x300.avi|mobile_352x288x30x420x300.avi" -y -map 0 -c copy concat.avi  
vlc concat.avi  
 
# Paste two videos  
ffmpeg -i coastguard_352x288x30x420x300.avi -i mobile_352x288x30x420x300.avi -c copy -map 0:0 -map 1:0 concat.avi  
vlc concat.avi

Chapter 8
Transcoding

8.1 Bit-rate

# What’s inside a file:  
ffmpeg -i input.mkv  
 
# Set the video bitrate of the output file to 64 kbit/s:  
$ ffmpeg -i input.avi -b:v 64k -bufsize 32k output.avi

8.2 Frame-rate

# Force the frame rate of the output file to 24 fps:  
ffmpeg -i input.avi -r 24 output.avi  
 
# Force the frame rate of the input file (valid for raw formats  
# only) to 1 fps and the frame rate of the output file to 24 fps:  
ffmpeg -r 1 -i input.m2v -r 24 output.avi

Chapter 9
Streaming

9.1 One-to-one unicast

# Sender:  
 
## UDP:  
ffmpeg -f alsa -i hw:0,0 -acodec mp2 -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -f mpegts udp://localhost:8888 # Runme first!  
 
## TCP:  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -f mpegts tcp://localhost:8888 # Runme last!  
 
## RTP/RTSP  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -f rtsp -rtsp_transport tcp rtsp://localhost:8888/live.sdp # Runme last!  
 
# Receiver:  
 
## UDP:  
ffplay -i udp://localhost:8888 # Runme last!  
 
## TCP:  
ffplay -i tcp://localhost:8888?listen # Runme first!  
 
## RTP/RTSP  
ffplay -rtsp_flags listen rtsp://localhost:8888/live.sdp?tcp # Runme first!

9.2 Multicast

# Sender:  
 
## UDP:  
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -f mpegts udp://236.0.0.1:8888 # Runme first!  
 
# Receiver:  
 
## UDP:  
ffplay -i udp://236.0.0.1:8888 # Runme last!

9.3 One-to-many unicast

cat > ffserver.conf << EOF  
Port 8090  
# bind to all IPs aliased or not  
BindAddress 0.0.0.0  
# max number of simultaneous clients  
MaxClients 1000  
# max bandwidth per-client (kb/s)  
MaxBandwidth 10000  
# Suppress that if you want to launch ffserver as a daemon.  
NoDaemon  
 
<Feed feed1.ffm>  
File /tmp/feed1.ffm  
FileMaxSize 5M  
</Feed>  
 
# FLV output - good for streaming  
<Stream test.flv>  
# the source feed  
Feed feed1.ffm  
# the output stream format - FLV = FLash Video  
Format flv  
VideoCodec flv  
# this must match the ffmpeg -r argument  
VideoFrameRate 15  
# generally leave this is a large number  
VideoBufferSize 80000  
# another quality tweak  
VideoBitRate 200  
# quality ranges - 1-31 (1 = best, 31 = worst)  
VideoQMin 1  
VideoQMax 5  
VideoSize 352x288  
# this sets how many seconds in past to start  
PreRoll 0  
# wecams don’t have audio  
Noaudio  
</Stream>  
 
# ASF output - for windows media player  
<Stream test.asf>  
# the source feed  
Feed feed1.ffm  
# the output stream format - ASF  
Format asf  
VideoCodec msmpeg4  
# this must match the ffmpeg -r argument  
VideoFrameRate 15  
# generally leave this is a large number  
VideoBufferSize 80000  
# another quality tweak  
VideoBitRate 200  
# quality ranges - 1-31 (1 = best, 31 = worst)  
VideoQMin 1  
VideoQMax 5  
VideoSize 352x288  
# this sets how many seconds in past to start  
PreRoll 0  
# wecams don’t have audio  
Noaudio  
</Stream>  
 
##################################################################  
# SDP/multicast examples  
#  
# If you want to send your stream in multicast, you must set the  
# multicast address with MulticastAddress. The port and the TTL can  
# also be set.  
#  
# An SDP file is automatically generated by ffserver by adding the  
# ’sdp’ extension to the stream name (here  
# http://localhost:8090/test1-sdp.sdp). You should usually give this  
# file to your player to play the stream.  
#  
# The ’NoLoop’ option can be used to avoid looping when the stream is  
# terminated.  
<Stream test1-sdp.mpg>  
Format rtp  
Feed feed1.ffm  
#File "/usr/local/httpd/htdocs/test1.mpg"  
MulticastAddress 224.124.0.1  
MulticastPort 5000  
MulticastTTL 16  
NoLoop  
</Stream>  
EOF  
 
xterm -e "ffserver -f ./ffserver.conf" &  
 
xterm -e "ffmpeg -f alsa -i hw:0,0 -acodec mp2 -f video4linux2 -s 640x480 -r 30 -i /dev/video0 http://localhost:8090/feed1.ffm" &  
 
mplayer http://localhost:8090/test.asf