1. Akiba-Online is sponsored by FileJoker.

    FileJoker is a required filehost for all new posts and content replies in the Direct Downloads subforums.

    Failure to include FileJoker links for Direct Download posts will result in deletion of your posts or worse.

    For more information see
    this thread.
    Dismiss Notice

Introduction to ffmpeg and creating gif/webm clips

Discussion in 'Tutorials' started by kunoichi, Feb 19, 2017.

  1. kunoichi

    kunoichi Well-Known Member


    ffmpeg is a very universal video tool that can be used to convert videos, extract clips, create webms/gifs, apply filters and much more.
    ffmpeg can handle almost all video formats, by default it will try to convert the input file to the format you specify in the output filename using the containers default settings.

    A zip file with this guide, a clip example along with a few .bat file examples are also attached to this post.

    A gif clip where logo has been removed and ping-pong added.

    Download ffmpeg here:

    For windows, grab a compiled build from:

    If you grab a static linked version you can extract just the ffmpeg.exe from the bin folder of the zipfile as it contains everything.

    For detailed documentation go to:

    Extracting a clip from a file
    Use this command to extract a clip and re-encode file to h264/aac using default settings for quality:

    ffmpeg -i "GUILD-149.mp4" -ss 01:11:09.000 -to 01:11:11.000 -y "GUILD-149-clip.mp4"

    -i is input file -ss start, -to end, -y overwrites output file if it already exist (be careful!), finally the output filename/format is given.

    Extracting a clip from a file while keeping video and audio formats
    You can specify to copy the video and audio streams rather than re-encoding them, but you may run into keyframe issues with a blank image for a while at the start of the clip for certain formats.
    A keyframe is usually when an entire frame is refreshed in the video file, to save space the frames until the next keyframe will only contain the differences using various compression techniques.

    ffmpeg -i "GUILD-149.mp4" -ss 01:11:05.000 -to 01:11:11.000 -c copy -y "GUILD-149-clip.mp4"

    -c copies both audio and video without re-encoding.

    Changing container format without re-encoding
    ffmpeg -i "GUILD-149.mp4" -c:v copy -c:a copy -y "GUILD-149.mkv"

    You can change container format simply by changing the filename and specifying -c copy,
    but in this case there was an issue with additional streams in the mp4 container.
    To fix this, -c:v copy was used to copy the video stream and -c:a copy to copy the audio stream without re-encoding.
    The chapters was converted to the mkv as well.

    Extracting a clip from a file and converting to webm
    When extracting clips it's a good idea to convert to a container format like webm that uses vp8 for video and vorbis for audio,
    that way you can post them to most imageboards in high quality and audio and you won't run into keyframe issues while working with the file.

    ffmpeg -i "GUILD-149.mp4" -ss 01:11:09.000 -to 01:11:11.000 -c:v libvpx -crf 4 -b:v 3000K -c:a libvorbis -y "GUILD-149-clip.webm"

    -c:v libvpx -crf 4 -b:v 3000K is used to specify vp8 video with constant rate factor 4 and 3000k bitrate. -c:a libvorbis is used to specify vorbis audio.

    Removing audio from a clip
    ffmpeg -i "GUILD-149-clip.webm" -c:v copy -an -y "GUILD-149-clip-silent.webm"

    -an is used to specify no audio (skip audio)

    Making a Ping Pong looped clip
    ffmpeg -i "GUILD-149-clip.webm" -filter_complex "[0]reverse[r];[0][r]concat" -c:v libvpx -crf 4 -b:v 3000K -an -y "GUILD-149-clip-ping-pong.webm"

    -filter_complex "[0]reverse[r];[0][r]concat" is used to reverse and merge the revered clip with original.
    [0]reverse[r] reverses the input stream which is [0] and outputs as [r]
    ; is a separator between filters
    [0][r]concat takes the input stream [0] and the reversed stream [r] from previous filter and concatenates (merges) them together as our final output.

    Making a high quality gif
    Due to the gif color limitation, when creating high quality gifs two passes should be used to first figure out the best palette.

    ffmpeg -i "GUILD-149-clip.webm" -vf "fps=15,scale=426:240:flags=lanczos,palettegen" -y palette.png
    ffmpeg -i "GUILD-149-clip.webm" -i palette.png -lavfi "fps=15,scale=426:240:flags=lanczos [x]; [x][1:v] paletteuse" -y GUILD-149-clip.gif

    First pass generates the best looking palette using -vf "fps=15,scale=426:240:flags=lanczos,palettegen"
    Second pass creates gif using the clip and palette as input using -lavfi "fps=15,scale=426:240:flags=lanczos [x]; [x][1:v] paletteuse"
    You can change scale and fps if you want better quality.

    Removing logos from clips
    If there are annoying logos in a video they can be censored using a delogo filter which will apply a type of blur to the logo region.
    The video has to be re-encoded but the audio can be copied.
    To find the logo region to be used a screenshot can be saved using a video player like Media Player Classic,
    load up the screenshot in a paint tool like Paint.NET and use the selection rectangle to find x,y position and width/height.

    ffmpeg -i "GUILD-149-clip.webm" -vf "delogo=x=19:y=446:w=133:h=25:band=3:show=0:enable='between(t,0,1.1)'" -c:v libvpx -crf 4 -b:v 3000K -c:a copy -y "GUILD-149-clip-nologo.webm"

    show=1 can be used with the delogo filter to draw a green rectangle around the blur area for testing purposes.
    enable='between(t,0,1.1)' applies the filter only from 0 seconds to 1.1 seconds.
    You can also chain multiple delogos at different times by building a filter pipeline:
    -vf "delogo=x=19:y=446:w=133:h=25:band=3:show=0:enable='between(t,0,1.1)'[0];[0]delogo=x=19:y=446:w=133:h=25:band=3:show=0:enable='between(t,1.5,2)'"

    Attached Files:

    Last edited: Feb 19, 2017
  2. kunoichi

    kunoichi Well-Known Member

    ffmpeg is improving all the time with additional filters and features!

    I have added a few more useful commands below

    Losslessly adjust the rotation of mp4 videos (useful for vertical videos)
    ffmpeg -i input.mp4 -metadata:s:v rotate="90" -codec copy output.mp4
    ffmpeg -i input.mp4 -metadata:s:v rotate="-90" -codec copy output.mp4
    Many recent video players will rotate the video according to the metadata

    Re-encode a 30fps interlaced video to 60fps progressive using yadif:

    ffmpeg -i input.vob -vf yadif=1 -c:v libx264 -preset veryslow -crf 17 -c:a copy output.mp4

    Re-encode a 30fps interlaced videos to 30fps progressive using yadif:
    ffmpeg -i input.vob -vf yadif=0 -c:v libx264 -preset veryslow -crf 17 -c:a copy output.mp4

    Download m3u8 streams as .mp4:
    ffmpeg -referer "https://twitter.com/" -i "https://video.twimg.com/ext_tw_video/1082560374451269632/pu/pl/320x426/VXZFNGwH21vnByta.m3u8" -codec copy vid.mp4

    Download m3u8 streams as .ts (transport stream):
    ffmpeg -referer "https://twitter.com/" -i "https://video.twimg.com/ext_tw_video/1082560374451269632/pu/pl/320x426/VXZFNGwH21vnByta.m3u8" -codec copy vid.ts
    You can usually find the .m3u8 url and referrer if you press F12 in your browser and open the developer network tab before visiting a web page.
    Last edited: Jan 9, 2019