Skip to content

Instantly share code, notes, and snippets.

@Orangestar12
Last active January 21, 2025 10:57

Revisions

  1. Orangestar12 revised this gist Jan 7, 2025. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,9 @@ Intel users can use "QuickSync" for hardware acceleration.

    ffmpeg -hwaccel qsv -c:v h264_qsv -i file.mkv -c:a aac output.mp4

    For AMD... it's complicated. [Check the wiki.](https://trac.ffmpeg.org/wiki/HWAccelIntro)
    AMD users should use the AMF encoder.

    ffmpeg -c:v h264_amf -i file.mkv -c:a aac output.mp4

    ## To WebM

  2. Orangestar12 revised this gist Jul 23, 2024. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,11 @@ Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, a
    - ~~Update: *unless* you're uploading them to Twitter. Twitter doesn't like MP4 files with MP3 audio. Fall back to `AAC` and provide a higher quality alternative somewhere else.~~
    - `libfdk_aac` is actually extremely uncommon in ffmpeg builds because `>nonfree` and a lot of sites don't like libmp3lame audio in MP4 containers. If you MUST use MP4 presumably the quality loss you'd get from using `aac` is acceptable. Otherwise, use MP3 if you need quality and *need* an MP4. Otherwise, try another container. (I keep my source videos in Matroska, personally.)
    - For MP4 encoders, use `-preset X` to use MP4 enc presets, like `slow` or `superfast`. (`veryfast` or `fast` is ok).
    - [`h264_nvenc`](#to-mp4) has its own presets, profiles, etc, and they're different for each GPU. You can check these with `ffmpeg -hide_banner -h encoder=h264_nvenc`
    - [`h264_nvenc`](#to-mp4) has its own presets, profiles, etc, and they're different for each GPU. You can check these with `ffmpeg -h encoder=h264_nvenc`
    - `c:v` refers to the video codec used (codec: video). Likewise, `c:a` is audio. If you're using `-map` or something, this can be extended (`-c:a:0`: codec: audio: stream 0; `-map 0:a:0`: file 0: audio: stream 0)
    - If you leave out any option, ffmpeg will either use a default or try to guess what you want based on the other options. I.e. if the container is mp4 it'll try encoding your audio with `aac` unless you specify.
    - To get rid of a stream, do `-an` or `-vn`, which will output a file with no audio or video stream respectively. If your audio file is silent you will save a LOT of filesize doing `-an`.
    - To hide the gigantic banner that shows at the start of every ffmpeg session, pass `-hide_banner`.

    ---

    @@ -51,6 +52,12 @@ Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, a
    If you have an Nvidia card and are running proprietary Nvidia drivers, you can use NVENC to speed up your encoding by an absolute crapton. (This is the same tech that runs "shadowplay" on Windows).

    ffmpeg -i file.mkv -c:v h264_nvenc -c:a aac output.mp4

    Intel users can use "QuickSync" for hardware acceleration.

    ffmpeg -hwaccel qsv -c:v h264_qsv -i file.mkv -c:a aac output.mp4

    For AMD... it's complicated. [Check the wiki.](https://trac.ffmpeg.org/wiki/HWAccelIntro)

    ## To WebM

  3. Orangestar12 revised this gist Aug 31, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    *If this is too much for you, check out [ffmpeg.guide](https://ffmpeg.guide/)!*
    *If this is too much for you, check out [ffmpeg.lav.io](https://ffmpeg.lav.io/)*

    These are a few quick easy ffmpeg command lines that can be used to make oft-used video formats. I use them a lot so I wrote them down in a txt file but I converted it to markdown to upload here and access on all my pcs.

  4. Orangestar12 revised this gist Aug 31, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # If this is too much for you, check out https://ffmpeg.guide/ !!!!!!! YEAH FFMPEG CLI GUI THIS IS A THING NOW
    *If this is too much for you, check out [ffmpeg.guide](https://ffmpeg.guide/)!*

    These are a few quick easy ffmpeg command lines that can be used to make oft-used video formats. I use them a lot so I wrote them down in a txt file but I converted it to markdown to upload here and access on all my pcs.

  5. Orangestar12 revised this gist Mar 25, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -130,7 +130,7 @@ Use [`-map`](https://trac.ffmpeg.org/wiki/Map).

    ffmpeg -loop 1 -i image.png -i audio.wav \
    -c:a aac -c:v libx264 -tune stillimage \
    -shortest -pix_fmt yuv420p +out.mp4
    -shortest -pix_fmt yuv420p out.mp4

    `-shortest` makes sure it stops when the audio stream ends and doesn't just encode until it runs out of space or crashes.

  6. Orangestar12 revised this gist Mar 16, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -130,7 +130,7 @@ Use [`-map`](https://trac.ffmpeg.org/wiki/Map).

    ffmpeg -loop 1 -i image.png -i audio.wav \
    -c:a aac -c:v libx264 -tune stillimage \
    -shortest out.mp4
    -shortest -pix_fmt yuv420p +out.mp4

    `-shortest` makes sure it stops when the audio stream ends and doesn't just encode until it runs out of space or crashes.

  7. Orangestar12 revised this gist Nov 17, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # If this is too much for you, check out https://ffmpeg.guide/ !!!!!!! YEAH FFMPEG CLI GUI THIS IS A THING NOW

    These are a few quick easy ffmpeg command lines that can be used to make oft-used video formats. I use them a lot so I wrote them down in a txt file but I converted it to markdown to upload here and access on all my pcs.

    Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, and personal experience.
  8. Orangestar12 revised this gist Mar 27, 2022. 1 changed file with 126 additions and 34 deletions.
    160 changes: 126 additions & 34 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -2,43 +2,95 @@ These are a few quick easy ffmpeg command lines that can be used to make oft-use

    Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, and personal experience.

    ---

    # General notes

    - Add `-movflags faststart` to make mp4 files have their headers at the beginning of the file, allowing them to be streamed (i.e. played even if only part of the file is downloaded).
    - The MP4 container supports MP3 files, so if `libfdk_aac` isnt available (it's the only good AAC enc) use `libmp3lame`.
    - Update: *unless* you're uploading them to Twitter. Twitter doesn't like MP4 files with MP3 audio. Fall back to `AAC` and provide a higher quality alternative somewhere else.
    - For MP4 files, use `-preset X` to use MP4 enc presets, like slow or superfast. (veryfast or fast is ok)
    - ~~Add `-movflags faststart` to make mp4 files have their headers at the beginning of the file, allowing them to be streamed (i.e. played even if only part of the file is downloaded).~~ EDIT 3/26/2022: I actually haven't used this flag in a long time. I think ffmpeg does it by default now.
    - ~~The MP4 container supports MP3 files, so if `libfdk_aac` isnt available (it's the only good AAC enc) use `libmp3lame`.~~
    - ~~Update: *unless* you're uploading them to Twitter. Twitter doesn't like MP4 files with MP3 audio. Fall back to `AAC` and provide a higher quality alternative somewhere else.~~
    - `libfdk_aac` is actually extremely uncommon in ffmpeg builds because `>nonfree` and a lot of sites don't like libmp3lame audio in MP4 containers. If you MUST use MP4 presumably the quality loss you'd get from using `aac` is acceptable. Otherwise, use MP3 if you need quality and *need* an MP4. Otherwise, try another container. (I keep my source videos in Matroska, personally.)
    - For MP4 encoders, use `-preset X` to use MP4 enc presets, like `slow` or `superfast`. (`veryfast` or `fast` is ok).
    - [`h264_nvenc`](#to-mp4) has its own presets, profiles, etc, and they're different for each GPU. You can check these with `ffmpeg -hide_banner -h encoder=h264_nvenc`
    - `c:v` refers to the video codec used (codec: video). Likewise, `c:a` is audio. If you're using `-map` or something, this can be extended (`-c:a:0`: codec: audio: stream 0; `-map 0:a:0`: file 0: audio: stream 0)
    - If you leave out any option, ffmpeg will either use a default or try to guess what you want based on the other options. I.e. if the container is mp4 it'll try encoding your audio with `aac` unless you specify.
    - To get rid of a stream, do `-an` or `-vn`, which will output a file with no audio or video stream respectively. If your audio file is silent you will save a LOT of filesize doing `-an`.

    ---

    # TOC
    - [Generic conversions](#generic-conversions)
    - [To MP4](#to-mp4)
    - [To WebM](#to-webm)
    - [Discard video, save audio to MP3](#discard-video-save-audio-to-mp3)
    - [Directory of images to video w/ audio](#directory-of-images-to-video-w-audio)
    - [Merge one video file and one audio file together](#merge-one-video-file-and-one-audio-file-together)
    - [If your video file already has audio](#if-your-video-file-already-has-audio)
    - [One image to the entire length of audio](#one-image-to-the-entire-length-of-audio)
    - [Other goofy stuff](#other-goofy-stuff)
    - [Add album art to MP3 file](#add-album-art-to-mp3-file)
    - [Convert from X to Y and keep the metadata](#convert-from-x-to-y-and-keep-the-metadata)
    - [More on video quality](#more-on-video-quality)
    - [Constant bitrate with `-b`](#constant-bitrate-with-b)
    - [Variable bitrate](#variable-bitrate)
    - [Quick editing commands](#quick-editing-commands)
    - [Trimming and seeking](#trimming-and-seeking)
    - [File Size](#file-size)
    - [Scaling](#scaling)

    ---

    # Generic conversions

    ## To MP4

    ffmpeg -i file.mkv -c:v libx264 -c:a libfdk_aac output.mp4
    ffmpeg -i file.mkv -c:v libx264 -c:a aac output.mp4

    If you have an Nvidia card and are running proprietary Nvidia drivers, you can use NVENC to speed up your encoding by an absolute crapton. (This is the same tech that runs "shadowplay" on Windows).

    ffmpeg -i file.mkv -c:v h264_nvenc -c:a libfdk_aac output.mp4
    ffmpeg -i file.mkv -c:v h264_nvenc -c:a aac output.mp4

    ## To WebM

    ffmpeg -i file.mkv -c:v libvpx-vp9 -b:v 1M -c:a libvorbis output.webm
    The default WebM audio encoder is `libopus` but you can also use `libvorbis`, which I personally prefer since it lets you easily stream copy them to `ogg` files.

    There are actually two WebM video encoders: The newer, faster, smaller VP9 codec and the older but more compatible VP8 codec.

    ### VP9

    ffmpeg -i file.mkv -c:v libvpx-vp9 -crf 28 -c:a libvorbis output.webm

    Change the bitrate to change the quality. Alternatively, replace with `-crf 10` to get a higher encoding rate but better overall quality.
    Turn the `-crf` parameter down to increase the quality at the expense of filesize.

    Use `-an` instead of a seperate audio codec to remove audio (for 4chan and gfycat and such).
    [VP9 is more useful with a two-pass encoding, but holy crap is it confusing and time-consuming.](https://trac.ffmpeg.org/wiki/Encode/VP9#twopass)

    # Discard video, save audio to MP3
    ### VP8

    ffmpeg -i file.mkv -c:v libvpx -qmin 4 -qmax 24 -c:a libvorbis output.webm

    As far as I'm aware the only time you'd *need* to use VP8 in the current year is uploading a WebM to 4chan.

    `-qmin` and `-qmax` set the file size.
    - Turn `-qmax` down if you need more quality.
    - Turn `-qmin` up if you need a smaller file.
    - The range is 0-63.

    You should *always* specify at least a `-qmax` because the default `-qmax` is 63, which means that if you don't specify it ffmpeg will literally try to compress the video as much as possible which will look like garbage.

    [If you want more info, check the section on video quality.](#more-on-video-quality)

    ## Discard video, save audio to MP3

    ffmpeg -i file.mkv -vn -c:a libmp3lame -qscale:a # output.mp3

    Where `#` is the audio quality, from `0`-`9`.

    # Directory of images to video w/ audio:
    ## Directory of images to video w/ audio:

    ffmpeg -r [Framerate (24)] \
    -f image2 -s [Image size (1280x720)] -i image%04d.png \
    -c:v libx264 -crf 25 -pix_fmt yuv420p \
    -i audio.wav -c:a libfdk_aac output.mp4
    -i audio.wav -c:a aac output.mp4

    `%04d` represents how many 0s trail the end of your filenames before the extension. i.e. filenames that look like `houston000004.png` would be `houston%05d.png`

    @@ -52,9 +104,9 @@ If your directory of images doesn't start at 0, you need to include `-start_numb

    ffmpeg -r 24 -f image2 -s 1280x720 -i image%04d.png \
    -c:v libx264 -crf 25 -pix_fmt yuv420p \
    -i audio.wav -c:a libfdk_aac output.mp4
    -i audio.wav -c:a aac output.mp4

    # Merge one video file and one audio file together
    ## Merge one video file and one audio file together

    ffmpeg -i video.mp4 -i audio.mp3 -c:v libx264 -c:a libmp3lame out.mp4

    @@ -64,38 +116,91 @@ Though, that's a little overkill. If you're using an mp4 and an mp3 you can just

    Containers like MKV can handle a lot of codecs, so `copy` is pretty versatile there.

    ## If your video file already has audio
    ### If your video file already has audio

    Use [`-map`](https://trac.ffmpeg.org/wiki/Map).

    ffmpeg -i video.mp4 -i audio.mp3 \
    -c:v libx264 -c:a libmp3lame \
    -map 0:v:0 -map 1:a:0 out.mp4

    # One image to the entire length of audio
    ## One image to the entire length of audio

    ffmpeg -loop 1 -i image.png -i audio.wav \
    -c:a libfdk_aac -c:v libx264 -tune stillimage \
    -c:a aac -c:v libx264 -tune stillimage \
    -shortest out.mp4

    `-shortest` makes sure it stops when the audio stream ends and doesn't just encode until it runs out of space or crashes.

    You may need to do -s, -pix_fmt, etc depending on your image.

    # Add album art to mp3 file:
    ---

    # Other goofy stuff

    ## Add album art to MP3 file

    ffmpeg -i file.mp3 -i artwork.png -map 0:0 -map 1:0 \
    -c copy -id3v2_version 3 -metadata:s:v title="Album cover" \
    -metadata:s:v comment="Cover (Front)" out.mp3

    There's a lot more metadata that can be added to an mp3 file but they're all obscure and weird and Cover is basically the only one you'll ever see used besides Lyrics (though I think lyrics are on mp4, not mp3)

    # Convert from X to Y and keep the metadata
    ## Convert from X to Y and keep the metadata

    ffmpeg -i "whatever.ogg" -ab 320k -map_metadata 0:s:0 "whatever.mp3"

    `-map_metadata` is a nice little command that does just what it says: maps the metadata from one stream to another. In this case we're maping the metadata from output stream 0 into input stream 0. It works like the normal `-map` command.

    ---

    # More on video quality

    This was touched on earlier but here's a full bit on making the most of the video quality settings.

    ## Constant bitrate with `-b`

    The easiest way to mess with quality is to specify a bitrate with `-b:v` or `-b:a` (again, video and audio respectively.) This results in a constant bitrate (CBR): I.E. the video streams exactly that many bits every second. If your video traffic is generally the same throughout the entire video or you don't want to exceed some encoding limit for your hardware, CBR will work fine for you.

    ffmpeg -i some_high_quality_source_video_thats_like_700mb.mkv -b:v 5000k -c:v libx264 a_much_smaller_mp4_file.mp4

    **Pros**
    - Predictable filesize because you know exactly how many bits are going into the video (5Mbps + 10sec video == 50Mb video)
    - High-traffic segments get compressed more
    - As long as your encoder can handle the input bitrate, it will never choke
    - Easier command to remember

    **Cons**
    - Low-traffic sections are not as compressed
    - High-traffic sections can look like a blurry or blocky mess (the "Twitch stream FIREBLU" problem)
    - During a real-time stream or recording, if your encoder can't compress a frame fast enough, the buffer will keep doubling in size until it can start compressing two frames in less time than the input frames are being recorded.

    ## Variable bitrate

    Ok here's the hard one. First, why you should use VBR:

    If you have a section that has a lot of *low* traffic and a section that has a lot of *high* traffic, then to get the most optimal picture and filesize you'd want the video file to send less data during the low traffic areas. For example, a video of a computer mouse moving around on a desktop would only contain the data for changing those pixels that the mouse is moving past, but footage from a video game being launched *from* that desktop would need much more data being streamed. VBR is ideal for things like screencasts and other VOD recordings where you want everything to be crisp and clear.

    The issue is that there's no standard for what parameters each encoder uses, so you'll just have to look around online to figure out how to use it. For example, libx264 uses a single `-crf` flag to determine the quality, which is a number between `0` and `51` (lower number == higher quality)...

    ffmpeg -i some_high_quality_source_video_thats_like_700mb.mkv -c:v libx264 -preset slow -crf 20 a_compressed_boi.mp4

    ...but h264_nvenc uses the `-qmin` and `-qmax` flags to tell the encoder the minimum and maximum compression you want applied to the file.

    ffmpeg -i some_high_quality_source_video_thats_like_700mb.mkv -c:v h264_nvenc -qmin 18 -qmax 24 this_is_probably_fine.mp4

    [slhck has a great page here detailing how to turn on VBR for the most popular software encoders.](https://slhck.info/video/2017/02/24/vbr-settings.html)

    **Pros**
    - High quality footage on every frame.
    - Lower filesize on average than CBR videos
    - Standard on the MP4 container, so you can expect high support

    **Cons**
    - Filesize is mostly unpredictable. Treat it like a black box. If your encoding settings are too good for your filesize budget you won't know until you've already waited for it to encode the whole thing (or at least up to the [`-fs` parameter](#file-size))
    - Higher chance to get "in over your head" and choke the encoder when encoding real-time streams. Highly recommend that you do not use VBR with real-time streams. Do a two-pass with a lossless source recording first.

    ---

    # Quick editing commands

    @@ -178,9 +283,7 @@ other cool use cases for -fs:
    - you're getting some kind of infinite stream (like recording your desktop) and you want to upload the file in some hosting service that doesn't care about durations (like 4chan's webms or something)
    - your input stream has sections of "heavy traffic" that make the file size more unpredictable to change by simply using an mp4 preset or lowering the time.

    # Scaling

    Because [Scaling (resizing) with ffmpeg](https://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg) is woefully outdated.
    ## Scaling

    Scaling is a video filter, so the syntax is `-vf`. This is an older syntax (ffmpeg used to use `-vc` for "video codec", and will still recognize what you meant if you use `-vc`.) so pardon the confusion with `-c`. Part of this is because `-f` is already used for "Format" and can't be used for "Filter". Enough semantics.

    @@ -218,15 +321,4 @@ If you're scaling at anything that isn't the same aspect ratio, [you will need t

    ffmpeg -i input.mp4 -vf scale=1280:-1,setsar=1 out.mp4

    Some distortion can be expected since, you know, you're discarding, substituting, or duplicating pixels to achieve the effect, but it generally looks fine if you're scaling down. (Example: NVIDIA Share refuses to acknowledge one of my monitors is portrait-rotated, so it always records videos at 16:10 instead of 10:16. I can squish the width to get a 656x1050 video which has just enough clarity to be visible.)

    ---

    And of course, all of this can be put together for some spectacular commands.

    ffmpeg -ss 00:02:52 \
    -i video.mp4 -i audio.mp3 \
    -c:v libx264 -c:a libmp3lame \
    -map 0:v:0 -map 1:a:0 \
    -fs 2M -to 00:08:10 \
    -shortest out.mp4
    Some distortion can be expected since, you know, you're discarding, substituting, or duplicating pixels to achieve the effect, but it generally looks fine if you're scaling down. (Example: NVIDIA Share refuses to acknowledge one of my monitors is portrait-rotated, so it always records videos at 16:10 instead of 10:16. I can squish the width to get a 656x1050 video which has just enough clarity to be visible.)
  9. Orangestar12 revised this gist Nov 7, 2021. 1 changed file with 17 additions and 8 deletions.
    25 changes: 17 additions & 8 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -113,28 +113,37 @@ You can use `-sseof` to seek from the end of the file

    ffmpeg -sseof 00:00:15 -i input.mp4 out.mp4

    Note that if you use `-ss`/`-sseof` before you specify a file, ffmpeg won't try to "read" those first X seconds of video first, which makes encoding start faster. By contrast, doing `ffmpeg -i input.mp4 -ss 00:00:15 out.mp4` will read the first 15 seconds, then start encoding.

    If your input stream has timestamps already set right (it should) then seeking should work fine, but I have noticed some strange recording software (like OBS) will sometimes output FLV files with broken or missing timestamps, in which case you'll get a radically different time if you try to seek it. In these cases, reading should be used.

    ### Trim

    To stop encoding after a set amount of time, use `-t` to specify a duration.

    ffmpeg -i input.mp4 -t 00:00:30 out.mp4
    ffmpeg -t 00:00:30 -i input.mp4 out.mp4

    This is similar to setting an egg timer to stop after 30 seconds.

    To stop encoding after a set time in the video has passed, use `-to`.

    ffmpeg -i input.mp4 -to 00:08:37 out.mp4
    ffmpeg -to 00:08:37 -i input.mp4 out.mp4

    This is like scheduling a DVR to record from 6:00 PM to 6:30 PM.

    Note that without `-ss`, `-t` and `-to` are functionally similar.

    When you seek with SS instead of reading (i.e. `-ss 0:13 -i file` vs `-i file -ss 0:13`), the timestamp will be set to 00:00:00, meaning that `-t` and `-to` will, once again, be functionally similar. You can combine two `-ss` commands to both seek and read, allowing you to quickly reach finite sections of a video. Example:
    `-ss 2:14 -i input.mp4 -ss 0.4 -t 5` will record a 5 second clip at 02:14.5
    ### Seeking vs Reading

    When `-ss`, `-to`, etc is placed *before* an input file, ffmpeg won't try to "read" those first X seconds of video first, which makes encoding start faster. Ffmpeg will "seek" to that position in the file first before trimming or whatever you're doing to the file. By contrast, doing `ffmpeg -i input.mp4 -ss 00:00:15 out.mp4` will read the first 15 seconds, then start encoding.

    If your input stream has timestamps already set right (it should) then seeking should work fine, but I have noticed some strange recording software (like OBS) will sometimes output FLV files with broken or missing timestamps, in which case you'll get a radically different time if you try to seek it. In these cases, reading should be used.

    This results in a few caveats:

    - `ffmpeg -ss 0:30 -to 0:35 -i ...` will seek to 0:30 and record to 0:35 based on the timestamps in the file.
    - `ffmpeg -ss 0:30 -i whatever.mp4 -to 0:35` will seek to 0:30, then *continue the encoding operation for 35 seconds.* This is because you've reset the internal "timer" to 0 by seeking, then reading the file. In this instance, -t and -to are functionally identical.
    - `-t` basically does the same thing regardless of where you put it.

    I don't know how this is handled regarding multiple inputs yet (`-ss 0:30 -i file.mp4 -ss 0:40 -i file.mp3` I think seeks to 30 seconds in the video then 40 seconds in the audio) so I'm open for comments helping me out here.

    Basically, if your video file isn't malformed in some way, just make sure to put `-ss`, `-t`, and `-to` before the `-i` command.

    ## File Size

  10. Orangestar12 revised this gist Sep 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, a
    - The MP4 container supports MP3 files, so if `libfdk_aac` isnt available (it's the only good AAC enc) use `libmp3lame`.
    - Update: *unless* you're uploading them to Twitter. Twitter doesn't like MP4 files with MP3 audio. Fall back to `AAC` and provide a higher quality alternative somewhere else.
    - For MP4 files, use `-preset X` to use MP4 enc presets, like slow or superfast. (veryfast or fast is ok)
    - `c:v` refers to the video codec used (codec: video). Likewise, `c:a` is audio. If you're using `-map` or something, this can be extended (`-c:a:0`: codec: audio: stream 0)
    - `c:v` refers to the video codec used (codec: video). Likewise, `c:a` is audio. If you're using `-map` or something, this can be extended (`-c:a:0`: codec: audio: stream 0; `-map 0:a:0`: file 0: audio: stream 0)

    # Generic conversions
    ## To MP4
  11. Orangestar12 revised this gist Sep 2, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,10 @@ Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, a

    ffmpeg -i file.mkv -c:v libx264 -c:a libfdk_aac output.mp4

    If you have an Nvidia card and are running proprietary Nvidia drivers, you can use NVENC to speed up your encoding by an absolute crapton. (This is the same tech that runs "shadowplay" on Windows).

    ffmpeg -i file.mkv -c:v h264_nvenc -c:a libfdk_aac output.mp4

    ## To WebM

    ffmpeg -i file.mkv -c:v libvpx-vp9 -b:v 1M -c:a libvorbis output.webm
  12. Orangestar12 revised this gist Aug 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -86,7 +86,7 @@ You may need to do -s, -pix_fmt, etc depending on your image.

    There's a lot more metadata that can be added to an mp3 file but they're all obscure and weird and Cover is basically the only one you'll ever see used besides Lyrics (though I think lyrics are on mp4, not mp3)

    # Covert from X to Y and keep the metadata
    # Convert from X to Y and keep the metadata

    ffmpeg -i "whatever.ogg" -ab 320k -map_metadata 0:s:0 "whatever.mp3"

  13. Orangestar12 revised this gist Aug 10, 2020. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -86,6 +86,13 @@ You may need to do -s, -pix_fmt, etc depending on your image.

    There's a lot more metadata that can be added to an mp3 file but they're all obscure and weird and Cover is basically the only one you'll ever see used besides Lyrics (though I think lyrics are on mp4, not mp3)

    # Covert from X to Y and keep the metadata

    ffmpeg -i "whatever.ogg" -ab 320k -map_metadata 0:s:0 "whatever.mp3"

    `-map_metadata` is a nice little command that does just what it says: maps the metadata from one stream to another. In this case we're maping the metadata from output stream 0 into input stream 0. It works like the normal `-map` command.


    # Quick editing commands

    If you're actually gonna do extensive video editing, you should probably use the Blender VSE or something. But if you only need to do a little bit of work on a file, these should help.
    @@ -106,7 +113,7 @@ Note that if you use `-ss`/`-sseof` before you specify a file, ffmpeg won't try

    If your input stream has timestamps already set right (it should) then seeking should work fine, but I have noticed some strange recording software (like OBS) will sometimes output FLV files with broken or missing timestamps, in which case you'll get a radically different time if you try to seek it. In these cases, reading should be used.

    ## Trim
    ### Trim

    To stop encoding after a set amount of time, use `-t` to specify a duration.

  14. Orangestar12 revised this gist Jun 7, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,9 @@ Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, a
    # General notes

    - Add `-movflags faststart` to make mp4 files have their headers at the beginning of the file, allowing them to be streamed (i.e. played even if only part of the file is downloaded).
    - mp4 container supports mp3 files, so if `libfdk_aac` isnt available (it's the only good aac enc) use `libmp3lame`.
    - For mp4 files, use `-preset X` to use mp4 enc presets, like slow or superfast. (veryfast or fast is ok)
    - The MP4 container supports MP3 files, so if `libfdk_aac` isnt available (it's the only good AAC enc) use `libmp3lame`.
    - Update: *unless* you're uploading them to Twitter. Twitter doesn't like MP4 files with MP3 audio. Fall back to `AAC` and provide a higher quality alternative somewhere else.
    - For MP4 files, use `-preset X` to use MP4 enc presets, like slow or superfast. (veryfast or fast is ok)
    - `c:v` refers to the video codec used (codec: video). Likewise, `c:a` is audio. If you're using `-map` or something, this can be extended (`-c:a:0`: codec: audio: stream 0)

    # Generic conversions
  15. Orangestar12 revised this gist Jun 7, 2019. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -185,14 +185,20 @@ Doubling or halving the size of an image using `iw` and `ih`, the "input width"

    The default is a bicubic scaler. You can make the encoding faster with a lower quality scaler:

    ffmpeg -i input.mp4 -vf scale=1280:-1:sws_flags="neighbor" out.mp4
    ffmpeg -i input.mp4 -vf scale=1280:-1,sws_flags="neighbor" out.mp4

    or more beautiful with a higher quality one:

    ffmpeg -i input.mp4 -vf scale=1280:-1:sws_flags="lanczos" out.mp4
    ffmpeg -i input.mp4 -vf scale=1280:-1,sws_flags="lanczos" out.mp4

    [A list of all options are available here](https://ffmpeg.org/ffmpeg-scaler.html#sws_005fflags)

    If you're scaling at anything that isn't the same aspect ratio, [you will need to change the video's SAR and DAR](https://stackoverflow.com/questions/50346707/ffmpeg-scaling-not-working-for-video). This is easy enough by adding the `setsar=1` flag to your filters.

    ffmpeg -i input.mp4 -vf scale=1280:-1,setsar=1 out.mp4

    Some distortion can be expected since, you know, you're discarding, substituting, or duplicating pixels to achieve the effect, but it generally looks fine if you're scaling down. (Example: NVIDIA Share refuses to acknowledge one of my monitors is portrait-rotated, so it always records videos at 16:10 instead of 10:16. I can squish the width to get a 656x1050 video which has just enough clarity to be visible.)

    ---

    And of course, all of this can be put together for some spectacular commands.
  16. Orangestar12 revised this gist Jun 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -167,7 +167,7 @@ If you know precisely what size you want the output to be, you can just specify

    ffmpeg -i input.mp4 -vf scale=1280:720 out.mp4

    (Some OSes, like Windows, freak out if you use colons on them. If you're running one of those, do `-f:v "scale=1280:720"` instead.)
    (Some OSes, like Windows, freak out if you use colons on them. If you're running one of those, do `-vf "scale=1280:720"` instead.)

    If you want to retain the aspect ratio, use `-1` to make ffmpeg calculate the proper height or width respectively.

  17. Orangestar12 revised this gist Dec 15, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,8 @@ Where `#` is the audio quality, from `0`-`9`.

    If things are coming out really wrong, double check to make sure your pixel format is `yuv420p`, if not you might double check the proper `pix_fmt` command for your images, or just batch convert them with ImageMagick or smth idk

    If your directory of images doesn't start at 0, you need to include `-start_number [index]` so image2 knows which image to start at.

    ### The above command, without fields marked in braces

    ffmpeg -r 24 -f image2 -s 1280x720 -i image%04d.png \
  18. Orangestar12 revised this gist Feb 2, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -119,6 +119,9 @@ This is like scheduling a DVR to record from 6:00 PM to 6:30 PM.

    Note that without `-ss`, `-t` and `-to` are functionally similar.

    When you seek with SS instead of reading (i.e. `-ss 0:13 -i file` vs `-i file -ss 0:13`), the timestamp will be set to 00:00:00, meaning that `-t` and `-to` will, once again, be functionally similar. You can combine two `-ss` commands to both seek and read, allowing you to quickly reach finite sections of a video. Example:
    `-ss 2:14 -i input.mp4 -ss 0.4 -t 5` will record a 5 second clip at 02:14.5

    ## File Size

    If you want to limit your recording/encoding/transcoding to a file size instead, use `-fs`.
  19. Orangestar12 revised this gist Jan 24, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -101,6 +101,8 @@ You can use `-sseof` to seek from the end of the file

    Note that if you use `-ss`/`-sseof` before you specify a file, ffmpeg won't try to "read" those first X seconds of video first, which makes encoding start faster. By contrast, doing `ffmpeg -i input.mp4 -ss 00:00:15 out.mp4` will read the first 15 seconds, then start encoding.

    If your input stream has timestamps already set right (it should) then seeking should work fine, but I have noticed some strange recording software (like OBS) will sometimes output FLV files with broken or missing timestamps, in which case you'll get a radically different time if you try to seek it. In these cases, reading should be used.

    ## Trim

    To stop encoding after a set amount of time, use `-t` to specify a duration.
    @@ -169,7 +171,7 @@ If you want to retain the aspect ratio, use `-1` to make ffmpeg calculate the pr

    The width and height are calculated equations, and the scale filter exposes a set of constants you can use. Basically this means that you can write simple expressions using the original image's height and width (among other things) to do specific options.

    Doubling or halving the size of an image using `iw` and `ih`, the "input height" and "width":
    Doubling or halving the size of an image using `iw` and `ih`, the "input width" and "height":

    ffmpeg -i input.mp4 -vf scale=iw*2:ih*2 out.mp4
    ffmpeg -i input.mp4 -vf scale=iw*.5:iw*.5 out.mp4
  20. Orangestar12 created this gist Jul 12, 2017.
    198 changes: 198 additions & 0 deletions ffmpeg.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,198 @@
    These are a few quick easy ffmpeg command lines that can be used to make oft-used video formats. I use them a lot so I wrote them down in a txt file but I converted it to markdown to upload here and access on all my pcs.

    Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, and personal experience.

    # General notes

    - Add `-movflags faststart` to make mp4 files have their headers at the beginning of the file, allowing them to be streamed (i.e. played even if only part of the file is downloaded).
    - mp4 container supports mp3 files, so if `libfdk_aac` isnt available (it's the only good aac enc) use `libmp3lame`.
    - For mp4 files, use `-preset X` to use mp4 enc presets, like slow or superfast. (veryfast or fast is ok)
    - `c:v` refers to the video codec used (codec: video). Likewise, `c:a` is audio. If you're using `-map` or something, this can be extended (`-c:a:0`: codec: audio: stream 0)

    # Generic conversions
    ## To MP4

    ffmpeg -i file.mkv -c:v libx264 -c:a libfdk_aac output.mp4

    ## To WebM

    ffmpeg -i file.mkv -c:v libvpx-vp9 -b:v 1M -c:a libvorbis output.webm

    Change the bitrate to change the quality. Alternatively, replace with `-crf 10` to get a higher encoding rate but better overall quality.

    Use `-an` instead of a seperate audio codec to remove audio (for 4chan and gfycat and such).

    # Discard video, save audio to MP3

    ffmpeg -i file.mkv -vn -c:a libmp3lame -qscale:a # output.mp3

    Where `#` is the audio quality, from `0`-`9`.

    # Directory of images to video w/ audio:

    ffmpeg -r [Framerate (24)] \
    -f image2 -s [Image size (1280x720)] -i image%04d.png \
    -c:v libx264 -crf 25 -pix_fmt yuv420p \
    -i audio.wav -c:a libfdk_aac output.mp4

    `%04d` represents how many 0s trail the end of your filenames before the extension. i.e. filenames that look like `houston000004.png` would be `houston%05d.png`

    `-crf` controls quality. if you're having trouble getting a good picture try fiddling with it.

    If things are coming out really wrong, double check to make sure your pixel format is `yuv420p`, if not you might double check the proper `pix_fmt` command for your images, or just batch convert them with ImageMagick or smth idk

    ### The above command, without fields marked in braces

    ffmpeg -r 24 -f image2 -s 1280x720 -i image%04d.png \
    -c:v libx264 -crf 25 -pix_fmt yuv420p \
    -i audio.wav -c:a libfdk_aac output.mp4

    # Merge one video file and one audio file together

    ffmpeg -i video.mp4 -i audio.mp3 -c:v libx264 -c:a libmp3lame out.mp4

    Though, that's a little overkill. If you're using an mp4 and an mp3 you can just use the `copy` codec, which is faster, since mp4 container supports mp3.

    ffmpeg -i video.mp4 -i audio.mp4 -c:v copy -c:a copy output.mp4

    Containers like MKV can handle a lot of codecs, so `copy` is pretty versatile there.

    ## If your video file already has audio

    Use [`-map`](https://trac.ffmpeg.org/wiki/Map).

    ffmpeg -i video.mp4 -i audio.mp3 \
    -c:v libx264 -c:a libmp3lame \
    -map 0:v:0 -map 1:a:0 out.mp4

    # One image to the entire length of audio

    ffmpeg -loop 1 -i image.png -i audio.wav \
    -c:a libfdk_aac -c:v libx264 -tune stillimage \
    -shortest out.mp4

    `-shortest` makes sure it stops when the audio stream ends and doesn't just encode until it runs out of space or crashes.

    You may need to do -s, -pix_fmt, etc depending on your image.

    # Add album art to mp3 file:

    ffmpeg -i file.mp3 -i artwork.png -map 0:0 -map 1:0 \
    -c copy -id3v2_version 3 -metadata:s:v title="Album cover" \
    -metadata:s:v comment="Cover (Front)" out.mp3

    There's a lot more metadata that can be added to an mp3 file but they're all obscure and weird and Cover is basically the only one you'll ever see used besides Lyrics (though I think lyrics are on mp4, not mp3)

    # Quick editing commands

    If you're actually gonna do extensive video editing, you should probably use the Blender VSE or something. But if you only need to do a little bit of work on a file, these should help.

    ## Trimming and seeking

    ### Seek

    To start encoding a file part of the way through, use `-ss` to seek to it.

    ffmpeg -ss 00:00:15 -i input.mp4 out.mp4

    You can use `-sseof` to seek from the end of the file

    ffmpeg -sseof 00:00:15 -i input.mp4 out.mp4

    Note that if you use `-ss`/`-sseof` before you specify a file, ffmpeg won't try to "read" those first X seconds of video first, which makes encoding start faster. By contrast, doing `ffmpeg -i input.mp4 -ss 00:00:15 out.mp4` will read the first 15 seconds, then start encoding.

    ## Trim

    To stop encoding after a set amount of time, use `-t` to specify a duration.

    ffmpeg -i input.mp4 -t 00:00:30 out.mp4

    This is similar to setting an egg timer to stop after 30 seconds.

    To stop encoding after a set time in the video has passed, use `-to`.

    ffmpeg -i input.mp4 -to 00:08:37 out.mp4

    This is like scheduling a DVR to record from 6:00 PM to 6:30 PM.

    Note that without `-ss`, `-t` and `-to` are functionally similar.

    ## File Size

    If you want to limit your recording/encoding/transcoding to a file size instead, use `-fs`.

    ffmpeg -i input.mp4 -fs 5M out.mp4

    This would stop copying `input.mp4` when the file reached 5 megabytes.

    ## Putting them all together

    You can combine these commands to get clips out of videos.

    ffmpeg -i input.mp4 -t 00:02:00 -fs 1MB out.mp4

    This would trim your video to 2 minutes or 1 megabyte, whichever happens first.

    ffmpeg -ss 00:30:00 -i input.mp4 -to 02:00:00 out.mp4

    This would make you a new version of `input.mp4` that lasts 1 hour and 30 minutes, and starts 30 minutes in.

    `-fs` is extremely useful if your hosting service has a strong limit on your filesize and/or duration, but you want to press your luck to see how good a quality video you can get under those limits.

    ffmpeg -ss 00:02:52 -i input.mp4 -preset fast -fs 2M f.mp4
    ffmpeg -ss 00:02:52 -i input.mp4 -preset veryfast -fs 2M vf.mp4
    ffmpeg -ss 00:02:52 -i input.mp4 -preset superfast -fs 2M sf.mp4
    ffmpeg -ss 00:02:52 -i input.mp4 -preset ultrafast -fs 2M uf.mp4

    other cool use cases for -fs:

    - you're on a storage budget and don't want to eat all your drive space with video files
    - you're getting some kind of infinite stream (like recording your desktop) and you want to upload the file in some hosting service that doesn't care about durations (like 4chan's webms or something)
    - your input stream has sections of "heavy traffic" that make the file size more unpredictable to change by simply using an mp4 preset or lowering the time.

    # Scaling

    Because [Scaling (resizing) with ffmpeg](https://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg) is woefully outdated.

    Scaling is a video filter, so the syntax is `-vf`. This is an older syntax (ffmpeg used to use `-vc` for "video codec", and will still recognize what you meant if you use `-vc`.) so pardon the confusion with `-c`. Part of this is because `-f` is already used for "Format" and can't be used for "Filter". Enough semantics.

    If you know precisely what size you want the output to be, you can just specify it.

    ffmpeg -i input.mp4 -vf scale=1280:720 out.mp4

    (Some OSes, like Windows, freak out if you use colons on them. If you're running one of those, do `-f:v "scale=1280:720"` instead.)

    If you want to retain the aspect ratio, use `-1` to make ffmpeg calculate the proper height or width respectively.

    ffmpeg -i input.mp4 -vf scale=1280:-1 out.mp4
    ffmpeg -i input.mp4 -vf scale=-1:720 out.mp4

    The width and height are calculated equations, and the scale filter exposes a set of constants you can use. Basically this means that you can write simple expressions using the original image's height and width (among other things) to do specific options.

    Doubling or halving the size of an image using `iw` and `ih`, the "input height" and "width":

    ffmpeg -i input.mp4 -vf scale=iw*2:ih*2 out.mp4
    ffmpeg -i input.mp4 -vf scale=iw*.5:iw*.5 out.mp4

    [A list of constants and options for `scale` are available here](https://ffmpeg.org/ffmpeg-filters.html#toc-scale-1)

    The default is a bicubic scaler. You can make the encoding faster with a lower quality scaler:

    ffmpeg -i input.mp4 -vf scale=1280:-1:sws_flags="neighbor" out.mp4

    or more beautiful with a higher quality one:

    ffmpeg -i input.mp4 -vf scale=1280:-1:sws_flags="lanczos" out.mp4

    [A list of all options are available here](https://ffmpeg.org/ffmpeg-scaler.html#sws_005fflags)

    ---

    And of course, all of this can be put together for some spectacular commands.

    ffmpeg -ss 00:02:52 \
    -i video.mp4 -i audio.mp3 \
    -c:v libx264 -c:a libmp3lame \
    -map 0:v:0 -map 1:a:0 \
    -fs 2M -to 00:08:10 \
    -shortest out.mp4