Skip to content

Instantly share code, notes, and snippets.

@YaroslavB
Forked from Orangestar12/ffmpeg.md
Created March 30, 2021 07:57
Show Gist options
  • Save YaroslavB/b19c6acb587ce2c3af810f28d90a7cd0 to your computer and use it in GitHub Desktop.
Save YaroslavB/b19c6acb587ce2c3af810f28d90a7cd0 to your computer and use it in GitHub Desktop.

Revisions

  1. @Orangestar12 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
  2. @Orangestar12 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
  3. @Orangestar12 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"

  4. @Orangestar12 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.

  5. @Orangestar12 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
  6. @Orangestar12 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.
  7. @Orangestar12 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.

  8. @Orangestar12 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 \
  9. @Orangestar12 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`.
  10. @Orangestar12 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
  11. @Orangestar12 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