## page was renamed from FfMpeg #language en ---- ~-[[DebianWiki/EditorGuide#translation|Translation(s)]]: English - [[fr/ffmpeg|French]] -~ ---- ~+FFmpeg/Libav+~ is a suite of open source software in command line that permits managing of audio or video streams. With FFmpeg/Libav, you can make recordings, corrections with filters, or transcode media from one format to an other. Libav is a fork of FFmpeg that can be found in the Debian repositories. If you want to use ffmpeg, you will need to change the word "avconv" to "ffmpeg" in the different commands lines. The rest stay the same. <> = Installation = To install libav: {{{ apt-get install libav-tools }}} To install ffmpeg: {{{ apt-get install ffmpeg }}} Be careful, FFmpeg is not available on Debian 8 Jessie, but in [[https://packages.debian.org/source/stable-backports/ffmpeg|Backports]]. To find out which deposit the package is available, look here: debpkg:libav or here debpkg:ffmpeg . It also possible to compile it by following the instructions available here: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu . = Usecases = FFmpeg or Libav can be use from GUI like: * [[VLC]] * [[MPlayer]] * ... Or directly from the command line interface, as explained bellow. = Introduction = An introduction to the terminology of video is useful to understand the FFmpeg syntax. == Formats == The format is the container that allows the transport of video, audio and subtitles or as a file (mkv, mov ...) or via stream (MPEG TS). Inside a container can be inserted (mux) or extract (demux): *one or more video streams (a movie or tv channels...) *one or more sound streams (VO, VF, ...) *one or more streams of subtitles. (English, hearing impaired, ...) *plus metadatas (title, artist's name, ....) We talk about multiplexing different tracks or streams in a format. FFmpeg/Libav provide a list of supported format: {{{ avconv -formats }}} {{{ ... DE avi AVI (Audio Video Interleaved) DE ogg Ogg D matroska,webm Matroska / WebM E mov QuickTime / MOV D mov,mp4,m4a,3gp,3g2,mj2 QuickTime / MOV E webm WebM ... }}} *D. = Demuxing supported *.E = Muxing supported == Codecs == The codec is an algorithm used to encode the video or sound to suit the transport protocol (IP, DVB, file ...) including reducing the flow rate (Kbits / s). By using codecs, compression can result in the loss of perceivable quality in picture or sound. In the same way as for formats, FFmpeg / Libav lists the codecs that it can handle: {{{ avconv -codecs }}} == Filters == FFmpeg / Libav also has a significant base of filters that can modify the content of each stream, such as changing resolution, change the volume of a track, superimpose a logo etc .... {{{ avconv -filters }}} == Formats + Codecs + Filters == To summarize, FFmpeg / Libav can multiplex or de-multiplex in different formats: * Compressed video streams (or not) * Compressed audio stream (or not) * Subtitles in various formats. And modify, using filters, the content of each stream independently. But if FFmpeg / Libav can handle tons of formats and tons of different codecs, all combinations are not possible, as shown in this table: https://www.videolan.org/streaming-features.html. Indeed, each codec or format has its own standard with more or less restrictive licenses, all combinations are not possible. Fortunately the free world provides several formats as well as several free codecs: * In formats: mkv, webm, ogv, ogg ... * In the video codecs: VP9, VP8, theora (vp3) dirac ... * In the audio codecs: flac, opus, vorbis, and many other ... = Knowing the contents of a file = Before starting any encoding it is good to know its contents, ffprobe or avprobe can read the header of the "format": {{{ avprobe tears_of_steel.mkv }}} Which return: {{{ Input #0, matroska,webm, from 'tears_of_steel.mkv': Metadata: title : ARTIST : COMPOSER : SYNOPSIS : DATE_RELEASED : GENRE : ENCODER : Lavf54.29.104 Duration: 00:12:14.12, start: 0.000000, bitrate: 4615 kb/s Stream #0:0(eng): Video: h264 (Main), yuv420p, 1280x534 [SAR 1:1 DAR 640:267], 24 fps, 24 tbr, 1k tbn, 180k tbc (default) Stream #0:1(eng): Audio: aac, 44100 Hz, stereo, s16 (default) Stream #0:2(eng): Audio: ac3, 48000 Hz, 5.1(side), s16, 448 kb/s (default) Stream #0:3(eng): Subtitle: ssa (default) Stream #0:4(fr): Subtitle: ssa (default) }}} So for this file, we can see that: * The input 0 describes the file format (Matroska here). (Some formats allow multiple input, hence the numbering) * The file includes 5 streams numbered from 0 to 4: * an encoded video stream in h264 * an audio stream in English stereo * a second audio stream in English 5.1 * and two subtitle one in English and one in French. = Change the Format = You can easily change format without affecting the stream: {{{ avconv -i tears_of_steel.mkv -c copy tears_of_steel.mov }}} * -i specifies the input file * -c copy: duplicate all streams without changing them * -c: v Copy: duplicate the video tracks without changing them * -c: a copy: duplicate identical audio tracks without changing them * -c: s copy: duplicate identical tracks subtitles without changing them '' If we do not tells ffmpeg/libav to make an exact copy of streams, it will launch an encoding default profile. '' If we want to differentiate streams between them: * -c: v: 0 first video track * -c: a: 0 first audio track * -c: a 1 second audio track * -c s: 0 first track subtitle * -c s: 2 second subtitle track '' For files with more than 3 streams, it seems there is a bug with the "copy" function. Some streams are lost when copying, you have to use the mapping function to be able to copy all. '' = Organize the order of the streams (mapping) = {{{ Streams: _________ _________ _________ #0:0 | | | | | | +------->------->| decoder |--->| filters |--->| encoder |--->----+ | |_________| |_________| |_________| | __________ | _________ _________ _________ | ___________ | | | #0:1 | | | | | | | | | | Input #O |----+------->. ,-->| decoder |--->| filters |--->| encoder |--->----+----->| Output #O | |__________| | \ / |_________| |_________| |_________| | |___________| | X _______________________________________ | | #0:2 / \ | | | +------->ยด `-->| copy |--->----+ |_______________________________________| \________/ \______/ \________/ demuxer mapping muxer }}} ##{{ https://wiki.debian-facile.org/_media/file-R25c876eb2dff659bf9c5f2a5b572d153.png }} ffmpeg/libav can change the order of the stream to suit your uses with the parameter " -map ": {{{ avconv -i tears_of_steel.mkv -map 0:0 -map 0:2 -map 0:1 -map 0:4 -map 0:3 -c copy tears_of_steel-v2.mkv }}} In this command I inverted the two audio streams between them and also the subtitles. *-map 0: 1: Track 1 of the input 0 (tears_of_steel.mkv) *-map 0: 2: Track 2 of the input 0 (tears_of_steel.mkv) When running, ffmpeg indicate which intersection (mapping) it applies: {{{ Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:2 -> #0:1 (copy) Stream #0:1 -> #0:2 (copy) Stream #0:4 -> #0:3 (copy) Stream #0:3 -> #0:4 (copy) }}} = Add a stream = The parameter ''-map'' allow also to add a stream, in the following example a subtitle in Spanish (TOS-es.srt) in position 4: {{{ avconv -i tears_of_steel.mkv -i TOS-es.srt -map 0:0 -map 0:1 -map 0:2 -map 1:0 -map 0:3 -map 0:4 -c:v copy -c:a copy -metadata:s:s:0 language=esp tears_of_steel-v2.mkv }}} *-map 0:0 refer to the first stream of the first "input": the video of the file "tears_of_steel.mkv" *-map 1:0 refer to the first stream of the second "input": the subtitle of the file "TOS-es.srt" This allows to insert it where you want: * Stream #0:0(eng): Video: h264 (Main), yuv420p, 1280x534 [SAR 1:1 DAR 640:267], 24 fps, 24 tbr, 1k tbn, 180k tbc (default) * Stream #0:1(eng): Audio: aac, 44100 Hz, stereo, s16 (default) * Stream #0:2(eng): Audio: ac3, 48000 Hz, 5.1(side), s16, 448 kb/s (default) * Stream #0:3(esp): Subtitle: ssa (default) * Stream #0:4(eng): Subtitle: ssa (default) * Stream #0:5(fr): Subtitle: ssa (default) The option metadata can indicate the file language: * -metadata:s:s:0 language=esp = Extract un stream = Using the possibilities of mapping it is very easy to extract one stream from a file: {{{ avconv -i tears_of_steel.mkv -map 0:2 -acodec copy tears_of_steel_soundtrack.mkv }}} In this example, we extract just one audio track. == Extract a audio track in MP3 == {{{ avconv -i tears_of_steel.mkv -map 0:2 -acodec libmp3lame -ar 44100 -ac 2 -ab 192k tears_of_steel_soundtrack.mp3 }}} == Extract a subtitle == {{{ avconv -i tears_of_steel.mkv -map 0:4 -c:s srt tears_of_steel_FR.srt }}} == Extract a segment == FFmpeg/Libav can extract, pieces of media, stating a point of entry with '' '-ss'' '(-ss 00:06:46) and specifying a time with '''-t''' (-t 00:01:00). This function is very useful for making encoding tests to validate his command. Note that '''-ss''' and '''-t''' should be placed before the first input (-i). {{{ avconv -ss 00:06:46 -t 00:01:00 -i tears_of_steel.mkv -c copy tears_of_steel_extract.mkv }}} Sometimes the term '''-t''' is not taken into account. You can use the option '''-to''' by placing it next to the output file {{{ avconv -ss 00:06:46 -i tears_of_steel.mkv -c copy -to 00:01:00 tears_of_steel_extract.mkv }}} = Increase the number of threads = Depending on the codec, it's possible to run multiple threads processors on a transcode with the option '''threads''': {{{ avconv -threads 4 -i tears_of_steel.mkv -c copy tears_of_steel_extract.mkv }}} Here I force it to work on 4 threads, that can save me valuable time. '''-threads 0''' is the automatic mode and use the maximum of available threads, but it does not work with all the codecs. = Encoding videos = ||'''option'''|| '''details''' || ||-b || set bitrate (in bits/s) 200kbps by default || ||-sameq || use the bitrate of the source || ||-s || set the size of the image (-s 320x240) || ||-aspect ||sample aspect ratio (4:3, 16:9 or 1.3333, 1.7777) || ||-vcodec ou -c:v || to choose a codec || ||-pass || Select the pass number, one pass (-pass 1) or two (-pass 2) || ||-qmin ||minimum video quantizer scale (VBR) || ||-qmax || maximum video quantizer scale (VBR) || ||-f || Force input or output file format. || ||-r || set the frame rate || == Encode video in VP8 == VP8 is a open source codec promoted by Google, a good replacement to H264/MP4 {{{ avconv -i tears_of_steel_720p.mkv -c:v:0 libvpx -crf 10 -vb 4M -c:a copy tears_of_steel_vp8.mkv }}} Here, we use the '''libvpx''' with two options: *-crf defines a quality level between 0 and 63 (small number = better quality but more time to process) *-vb 4M gives a target rate to 4 Megabit/s A list of possible options for encoding in VP8 is available here: * [[http://wiki.webmproject.org/ffmpeg]] == Encode video in VP9 == VP9 is a open source codec promoted by Google, to compete with h265/HEVC. {{{ avconv -i tears_of_steel_720p.mkv -c:v:0 libvpx -crf 10 -vb 4M -c:a copy tears_of_steel_vp8.mkv }}} Here, we use the '''libvpx-vp9''' with three options: *-crf defines a quality level between 0 and 63 (small number = better quality but more time to process) *-vb 4M gives a target rate to 4 Megabit/s *-threads is necessary to force the number of core to use, libvpx-vp9 doesn't detect them yet. A list of possible options for encoding in VP9 is available here: * [[http://wiki.webmproject.org/ffmpeg]] * [[http://wiki.webmproject.org/ffmpeg/vp9-encoding-guide]] vlc has an issue to decode VP9, see here: https://trac.videolan.org/vlc/ticket/11763 But it's possible to read it with ffplay: {{{ ffplay tears_of_steel_vp9.mkv }}} == Encode video in H264/Mpeg4 == The H264/MPEG4 is a proprietary codec commonly used for its good quality/speed ratio: {{{ avconv -i tears_of_steel_720p.mkv -c:v:0 libx264 -preset slow -crf 22 -c:a copy tears_of_steel_h264.mkv }}} * '''-preset''' sets a encoding speed, more it will be slow better the image quality will be. Are available: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower et veryslow. Medium is the default setting. * '''-crf''' set a quality level between 0 and 51 (small number = better quality but more time to process) A more detailed documentation is available here: https://trac.ffmpeg.org/wiki/Encode/H.264 == Encode video in H.265/HEVC == The H.265/HEVC is a new proprietary codec, futur successor of the H264/Mpeg4. The codec that manage it are also new, and all the options and optimisations are not available. To get a better speed of encoding and a better image quality, try to get a newer version of the codec. {{{ avconv -i tears_of_steel_720p.mkv -c:v:0 libx265 -preset slow -crf 22 -c:a copy tears_of_steel_h265.mkv }}} A more detailed documentation is available here: https://trac.ffmpeg.org/wiki/Encode/H.265 and here http://x265.readthedocs.org/en/default/ = Encoding sound = The list of available option for transcoding sound can be found here: [[https://ffmpeg.org/ffmpeg.html#Audio-Options]] The main options: ||'''options'''||'''details''' || ||-acodec ou -c:a || to choose a codec || ||-ar || set the sample frequency (44100 Hz) || ||-ab || set the bitrate, 64kbps by default || ||-ac || set the number of channels (mono-stereo || == Encode sound in Vorbis == Vorbis is an open source audio codec, with loss, equivalent to MP3/H264: {{{ avconv -i tears_of_steel_720p.mkv -c:v copy -c:a:0 libvorbis -qscale:a 5 -ar 48000 tears_of_steel_vorbis.mkv }}} *-qscale defines the quality level between 0-10, where 10 is the best quality. By default, qscale is set to 3. *-ar 48000 allows to resample the audio in 48kHz. == Encode a audio file in MP3 == Several codecs can encode the audio to MP3, limp3lame is one of the most used. Here, an example forcing the bitrate to 256Kbits/s, resampling in 44100hz and forcing the production of a stereo channel: {{{ avconv -i 01.ogg -acodec libmp3lame -ar 44100 -ac 2 -ab 256k 01.mp3 }}} '' Warning metadata can be lost using this method '' = Still images = == Transform a image sequence into a video == {{{ avconv -f image2 -pattern_type glob -i "*.tif" -r 24 outputvideo.mkv }}} In this example, we transcode all the .tif files from the current folder into a outputvideo.mkv. But it's possible to use other format types such as: PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TGA, TIFF, SGI, PTX. *{{{-r 24}}} allows to specify the framerate Be careful to the debit, still images can be very heavy. You might need to add a compression behind: {{{ avconv -f image2 -pattern_type glob -i "*.tif" -r 24 -c:v:0 libvpx -crf 10 -vb 4M outputvideo.mkv }}} ##more translation from french needed = External Links = * FFmpeg Homepage: https://ffmpeg.org/ * FFmpeg Documentation: https://trac.ffmpeg.org/wiki * Libav Homepage: https://libav.org/