FFMPEG is an [[LGPLv2.1]] licensed media transcoder and manipulation tool written in [[3. Reference/Software/Programming Languages/C|C]]. - [Website](https://ffmpeg.org/) - [Source](https://git.ffmpeg.org/gitweb/ffmpeg.git/blob/HEAD:/README.md) - [Documentation](https://ffmpeg.org/documentation.html) - [AlternativeTo](https://alternativeto.net/software/ffmpeg/about/) > FFmpeg is a collection of libraries and tools to process multimedia content such as audio, video, subtitles and related metadata. # Notability One of the most important media tools available today. # Philosophy # Platform Support # Features # Tips ## OCR & TTS - https://dev.to/video/exploring-ocr-and-text-to-speech-in-ffmpeg-2o61 ## Losslessly Change Container Use `copy` with the audio `-c:a` and `-c:v` codec options.[^3][^4][^6] ```sh ffmpeg -i video.mkv -c:v copy -c:a copy -map 0 -sn output.mp4 ``` ## Remove Audio Use the `-an` flag.[^2] ```sh ffmpeg -i video.mkv -c:v copy -an output.mp4 ``` ## Extract Audio to File AKA remove video by `copy`ing the audio `-acodec` and removing the video `-vn`.[^5] ```sh ffmpeg -i video.mkv -vn -acodec copy -map a output.aac ``` The `-map a` is needed to tell `ffmpeg` not to worry about subtitles or any other video streams that won't fit into the target container. ## Extract DTS Audio to File [[Solus]]'s build of `ffmpeg` doesn't include the DTS/ADTS muxer. To build it in: ```sh git clone https://git.ffmpeg.org/ffmpeg.git cd ffmpeg ./configure --enable-muxer=adts make ``` ## Extract Text Subtitles ```sh ffmpeg -i video.mkv -map 0:s:0 subs.srt ``` ## Extract Bitmap Subtitles ```sh ffmpeg -i video.mkv -map 0:s:0 -c:s:0 copy subs.sup ``` Then you should download [[SubtitleEdit]] and use it to OCR the bitmaps and save it as an [[Subtitles|SRT]] file which is a plain text file. ## Remove Silence Sometimes sound files will have a long interval of silence at the beginning or end, using a tool like [[Audacity]] could remove them, but will result in quality loss from re-encoding. `ffmpeg` can solve this problem without introducing any additional quality loss. The `silenceremove` filter[^9] can do this. ```sh ffmpeg -i input.mp3 -af "silenceremove=start_periods=1:start_duration=0.25:start_threshold=-20dB:stop_periods=-1:stop_duration=0.25:stop_threshold=-20dB" output.mp3 ``` | | | | ----------------- | ------------------------------------------------------------ | | `start_periods` | Truncate silence found starting at the beginning of the file | | `start_duration` | The amount of time before considering a section for removal | | `start_threshold` | The level at which the audio is considered silent | | `stop_periods` | Truncate silence found starting from the end of the file | | `stop_...` | same as `start_...` | There are several other optional parameters that might be useful in more specialized situations for fine tuning. ### Detect Silence You can use the `silencedetect` filter[^8] if you just want a log of where the silences are to make sure your settings are good. Pass `-f null /dev/null` in place of the output file to avoid doing extra work. ## Merging Videos If you want to join two videos into one longer one, `ffmpeg` has got your back with the `concat` filter.[^10][^13] ### Same Codec If it's the same codec for each file then this is easy: ```sh ffmpeg -f concat -safe 0 -i playlist.txt -c copy output.mp4 ``` Or if the codec supports simple joining at the file level by treating each file as part of the same original file: *(untested by me)* ```sh ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy output.ts ``` ### Different Codecs You can re-encode source files to all be the same codec and use the above methods if the codecs differ. Or you can write a `filter_complex` which maps the different files to different streams or orders.[^11] ```sh ffmpeg -i input1.mp4 -i input2.webm -i input3.mov \ -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" \ -map "[outv]" -map "[outa]" output.mkv ``` Note that the streams are specified by index so if the input files have multiple streams you will need to check and make sure that you are using the right ones. #### List File Streams To list the file streams, either use `ffmprobe` or trick[^12] `ffmpeg` into telling you itself: ```sh ffmpeg -i file.mp4 2>&1 | rg "Stream" ``` ## Convert to WebM To preserve quality, do a two-pass conversion.[^1] The first pass we discard the video because we just need the metadata (`ffmpeg2pass-0.log`) and the second one uses that info to do the conversion. >[!WARNING] > This may take a *long time* even for short videos! > > It very much depends on the input video codec and framerate but a 2:34 60fps screen recording from OBS Studio took 8:51 to complete! ```sh #!/bin/sh -xe ffmpeg -i "$1" -b:v 0 -crf 30 -pass 1 -an -f webm -y /dev/null ffmpeg -i "$1" -b:v 0 -crf 30 -pass 2 "${1%.mp4}.webm" ``` ## FFPLAY `ffplay` is a tool that comes with `ffmpeg` that provides the ability to play and process files, including filters, and generate different effects such as video color vectorscope and audio stereo vectorscopes, spectrograms, and more.[^7] ### Audio Vectorscope ```sh ffplay -f lavfi 'amovie=input.flac,asplit=2[out1][a],[a]avectorscope=m=polar:s=800x400[out0]' ``` ## Creating Videos ### Animated Text - https://superuser.com/questions/874598/creating-video-containing-animated-text-using-ffmpeg-alone # Resources ## GUIs - https://sites.google.com/site/dmsimpleapps ## Forks A group of `ffmpeg` developers split off in 2012 and formed `avconv` which shipped its own versions of `ffmpeg` with error messages and libraries that confused a lot of people. The `avconv` fork was discontinued in 2019 and is now defunct. # References ## LibAV - https://stackoverflow.com/questions/9477115/what-are-the-differences-and-similarities-between-ffmpeg-libav-and-avconv - http://blog.pkh.me/p/13-the-ffmpeg-libav-situation.html ## Footnotes [^1]: https://video.stackexchange.com/questions/19590/convert-mp4-to-webm-without-quality-loss-with-ffmpeg [^2]: https://superuser.com/questions/268985/remove-audio-from-video-file-with-ffmpeg [^3]: https://superuser.com/questions/472420/handbrake-settings-to-convert-mkv-to-mp4-while-retaining-the-original-quality [^4]: https://askubuntu.com/questions/50433/how-to-convert-mkv-file-into-mp4-file-losslessly [^5]: https://stackoverflow.com/questions/42542906/ffmpeg-what-muxer-do-i-need-to-save-an-aac-audio-stream [^6]: https://github.com/jspw/Little-Fun-Stuffs/tree/main/mkv%20to%20mp4%20converter [^7]: https://trac.ffmpeg.org/wiki/FancyFilteringExamples [^8]: https://ffmpeg.org/ffmpeg-filters.html#silencedetect [^9]: https://ffmpeg.org/ffmpeg-filters.html#silenceremove [^10]: https://trac.ffmpeg.org/wiki/Concatenate [^11]: https://trac.ffmpeg.org/wiki/Concatenate#differentcodec [^12]: https://superuser.com/questions/1479702/how-to-list-streams-with-ffmpeg [^13]: https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg