ffmpeg is along with VLC (cvlc) essential tools in the video-guru’s toolkit.
Contents
Introduction to ffmpeg
A command line software package (command line interface (CLI), in comparison to GUI – Graphical User Interface application), that exists on all major platforms (macOS, Linux distros, MS Windows).
Amazingly powerful and capable!
- Handles all sorts of parts of media, like (order as used by mediainfo):
(see also classifications for more)- General – container
- Video stream(s)
- Audio stream(s)
- Text (subtitles)
- Other
- Image
- Menu
- Add, remove, replace, streams (add subtitles to own video)
- Clip parts – extract parts of long materials, cut out a 1 minute sample material for some longer production
- Concatenate files
- Convert between every possible format
- ‘Take a screenshot’ from a video, extract a specific frame, to jpeg, png
- …
Getting It, Installation
- macOS, once have brew installed:
- brew install ffmpeg
- installs in /usr/local/bin/
- Linux:
- sudo apt-get install ffmpeg
- Video DownloadHelper Companion App
- DownloadHelper, when installed, depend on a specific installation
- google.com/search?q=net.downloadhelper.coapp.app
- Is installed as:
/Applications/net.downloadhelper.coapp.app - And in folder:
/Applications/net.downloadhelper.coapp.app/Contents/MacOS/converter/build/mac/64/
has the following commands:- ffmpeg
- ffplay
- ffprobe
Some Initial Resources
- Home page: ffmpeg.org
- Documentation: ffmpeg.org/documentation.html
- linuxquestions.org/questions/linux-general-1/convert-mpg-to-mp4-4175514239/
- 20+ FFmpeg Commands For Beginners (2019-05-21)
ostechnix.com/20-ffmpeg-commands-beginners/ - search on ffmpeg/share – looking for where profiles maybe stored
google.com/search?q=ffmpeg%2Fshare
Convert, Transcode, .MPG to H.264 MP4
google.com/search?q=ffmpeg+convert+mov+to+mp4
opensource.com/article/17/6/ffmpeg-convert-media-file-formats
H.264 Video Encoding Guide – trac.ffmpeg.org/wiki/Encode/H.264
I.e., in the case of materials worked on in these examples,
- from files with extension .mpg,
- and in this case these are MPEG-2 materials
- to files I chose to use extension .mp4, container format that CAN have a variety of variants of MPEG materials including for instance:
- ‘MPEG-4’, ‘MPEG-4 Visual’ (MPEG-4 Part 2, H.263-compatible)
- H.264, AVC (MPEG-4 Part 10)
- H.265, HEVC (MPEG-H Part 2)
- (See also Technical (MPEG) for more on MPEG std.)
https://www.google.com/search?q=convert+mpeg2+to+mp4+ffmpeg
Simplest Transcoding
E.g., from https://www.linuxquestions.org/questions/linux-general-1/convert-mpg-to-mp4-4175514239/
ffmpeg -i input.mpg output.mp4
HandBrakeCLI -Z Universal -i input.mpg -o output.mp4
2-Pass or Not
google.com/search?q=ffmpeg+2-pass+or+crf
“1 pass is faster than 2 passes. 2-pass does not make a better quality or smaller file: it only lets you set the output file size (but not the quality), whereas -crf lets you choose the quality (but not the file size).” [superuser.com/questions/1362800/ffmpeg-2-pass-encoding#:~:text=1%20pass%20is%20faster%20than,but%20not%20the%20file%20size).]
gist.github.com/hsab/7c9219c4d57e13a42e06bf1cab90cd44
superuser.com/questions/1362800/ffmpeg-2-pass-encoding
google.com/search?q=ffmpeg+crf
superuser.com/questions/677576/what-is-crf-used-for-in-ffmpeg
Convert, Transcode, to H.265
Concatenate
google.com/search?q=ffmpeg+concat+videos
trac.ffmpeg.org/wiki/Concatenate
Cut Clip from Video
google.com/search?q=ffmpeg+extract+clip
-ss h:m:s.xxx
or-ss m.msec
: start at- importation distinction is WHERE placing on command line, at least older ffmpeg
- before -i <src>: faster search
- after -i <src>: slower but more accurate
- (source superuser.com/questions/138331/using-ffmpeg-to-cut-up-video)
- importation distinction is WHERE placing on command line, at least older ffmpeg
- -t h:m:s.xxx : how much material to use
Example:
ffmpeg -i input.wmv -ss 60 -t 60 -acodec copy -vcodec copy output.wmv
Three different cases:
- From start to some point:
-i <in> -t <d> -c copy
- Mid-section, from – to…..:
-i <in> -ss <x> -t <d> -c copy
- From some point to end..:
-i <in> -ss <x> -c copy
Examples:
- ffmpeg -i input.mp4 -codec copy -t 0:01:00 1st-min.mp4
- ffmpeg -i input.mp4 -codec copy -ss 0:01:00 -t 0:01:00 2nd-min.mp4
Extract Audio
stackoverflow.com/questions/9913032/how-can-i-extract-audio-from-video-with-ffmpeg
x
en.wikipedia.org/wiki/Audacity_(audio_editor)
https://lame.buanzo.org/#lameosx64bitdl
Scaling
- google.com/search?q=ffmpeg+rescale+video
- Scaling – trac.ffmpeg.org/wiki/Scaling
- How can I crop a video with ffmpeg? – video.stackexchange.com/questions/4563/how-can-i-crop-a-video-with-ffmpeg
Snapshot, Frame
google.com/search?q=snapshot+from+video+ffmeg
Use the -ss
option:
ffmpeg -ss 01:23:45 -i input -vframes 1 -q:v 2 output.jpg
- For JPEG output use
-q:v
to control output quality. Full range is a linear scale of 1-31 where a lower value results in a higher quality. 2-5 is a good range to try. - The select filter provides an alternative method for more complex needs such as selecting only certain frame types, or 1 per 100, etc.
- Placing
-ss
before the input will be faster. See FFmpeg Wiki: Seeking and this excerpt from theffmpeg
cli tool documentation:
ffmpeg Command – Built-in Help
im2720:~ johan$ ffmpeg --help ffmpeg version 4.1.2 Copyright (c) 2000-2019 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.11.45.5) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr libavutil 56. 22.100 / 56. 22.100 libavcodec 58. 35.100 / 58. 35.100 libavformat 58. 20.100 / 58. 20.100 libavdevice 58. 5.100 / 58. 5.100 libavfilter 7. 40.101 / 7. 40.101 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 3.100 / 5. 3.100 libswresample 3. 3.100 / 3. 3.100 libpostproc 55. 3.100 / 55. 3.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... Getting help: -h -- print basic options -h long -- print more options -h full -- print all options (including all format and codec specific options, very long) -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf See man ffmpeg for detailed description of the options. Print help / information / capabilities: -L show license -h topic show help -? topic show help -help topic show help --help topic show help -version show version -buildconf show build configuration -formats show available formats -muxers show available muxers -demuxers show available demuxers -devices show available devices -codecs show available codecs -decoders show available decoders -encoders show available encoders -bsfs show available bit stream filters -protocols show available protocols -filters show available filters -pix_fmts show available pixel formats -layouts show standard channel layouts -sample_fmts show available audio sample formats -colors show available color names -sources device list sources of the input device -sinks device list sinks of the output device -hwaccels show available HW acceleration methods Global options (affect whole program instead of just one file: -loglevel loglevel set logging level -v loglevel set logging level -report generate a report -max_alloc bytes set maximum size of a single allocated block -y overwrite output files -n never overwrite output files -ignore_unknown Ignore unknown stream types -filter_threads number of non-complex filter threads -filter_complex_threads number of threads for -filter_complex -stats print progress report during encoding -max_error_rate maximum error rate ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success. -bits_per_raw_sample number set the number of bits per raw sample -vol volume change audio volume (256=normal) Per-file main options: -f fmt force format -c codec codec name -codec codec codec name -pre preset preset name -map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile -t duration record or transcode "duration" seconds of audio/video -to time_stop record or transcode stop time -fs limit_size set the limit file size in bytes -ss time_off set the start time offset -sseof time_off set the start time offset relative to EOF -seek_timestamp enable/disable seeking by timestamp with -ss -timestamp time set the recording timestamp ('now' to set the current time) -metadata string=string add metadata -program title=string:st=number... add program with specified streams -target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-") -apad audio pad -frames number set the number of frames to output -filter filter_graph set stream filtergraph -filter_script filename read stream filtergraph description from a file -reinit_filter reinit filtergraph on input parameter changes -discard discard -disposition disposition Video options: -vframes number set the number of video frames to output -r rate set frame rate (Hz value, fraction or abbreviation) -s size set frame size (WxH or abbreviation) -aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777) -bits_per_raw_sample number set the number of bits per raw sample -vn disable video -vcodec codec force video codec ('copy' to copy stream) -timecode hh:mm:ss[:;.]ff set initial TimeCode value. -pass n select the pass number (1 to 3) -vf filter_graph set video filters -ab bitrate audio bitrate (please use -b:a) -b bitrate video bitrate (please use -b:v) -dn disable data Audio options: -aframes number set the number of audio frames to output -aq quality set audio quality (codec-specific) -ar rate set audio sampling rate (in Hz) -ac channels set number of audio channels -an disable audio -acodec codec force audio codec ('copy' to copy stream) -vol volume change audio volume (256=normal) -af filter_graph set audio filters Subtitle options: -s size set frame size (WxH or abbreviation) -sn disable subtitle -scodec codec force subtitle codec ('copy' to copy stream) -stag fourcc/tag force subtitle tag/fourcc -fix_sub_duration fix subtitles duration -canvas_size size set canvas size (WxH or abbreviation) -spre preset set the subtitle options to the indicated preset im2720:~ johan$