FFmpeg Video Editing Essentials: Trimming, Merging, Subtitling, and More!

As an open-source tool, FFmpeg provides a wide array of functions for handling media files. Let's learn how to use FFmpeg to edit videos programmatically, including trimming, merging, adding subtitles, etc.
by Josephine Loo ·


    FFmpeg is a handy tool for developers to build video editing capabilities into their applications. This open-source software suite offers a comprehensive range of functions for manipulating video and audio files, making it a go-to choice for building video editing features. In this article, we'll explore some of the essential FFmpeg commands for common video editing tasks, such as trimming, merging, adding subtitles, and more!

    What is FFmpeg

    FFmpeg is a complete, cross-platform solution to record, convert, and stream audio and video. It can decode, encode, transcode, mux, demux, stream, filter, and play media files in any format. It is also highly portable—it compiles and runs in a wide variety of build environments, machine architectures, and configurations like Linux, Mac OS X, Microsoft Windows, etc.

    FFmpeg contains multiple tools for end-users to convert, play, and analyze media files and libraries for developers to use in different applications, such as libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale, and libswresample. When you download the FFmpeg packages/executable files to your machine, these libraries will be downloaded automatically.

    Installing FFmpeg

    To get started with FFmpeg, you'll first need to download and install it on your system. FFmpeg is open-source and supports multiple platforms, including Windows, macOS, and Linux. You can follow this step-by-step guide to install it on your system. Once you have FFmpeg installed, you can open your terminal/command prompt to begin using it.

    For reference, the FFmpeg version used in this tutorial is v6.0.

    Editing Videos Using FFmpeg

    Cropping/Resizing Videos

    FFmpeg’s crop filter allows you to crop videos into any dimension.

    The easiest way to use this filter is by specifying the width and height of the output video. FFmpeg will automatically crop the video into the specified dimension from the center:

    ffmpeg -i input.mp4 -vf "crop=w:h" output.mp4

    w - desired width in pixels

    h - desired height in pixels

    You can also specify the coordinate where the video will be cropped by adding the x:y options:

    ffmpeg -i input.mp4 -vf "crop=w:h:x:y" output.mp4

    🐻 Bear Tips: The coordinates start at (0,0) in the top left corner of the input video.

    Cropping Out Black Borders

    You can use the "cropdetect" filter to auto-detect the crop size and position of the black borders around a video. The filter will detect the input video's non-black area, and then calculate and print the recommended crop size and position.

    Here’s the command to get the crop size and position:

    ffmpeg -i input.mp4 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1

    You can then use the output result (highlighted in the red box) to crop your video and remove black borders:

    ffmpeg cropdetect result.png

    ffmpeg -i input.mp4 -vf "crop=1280:576:0:72" output.mp4

    Trimming Videos

    You can trim the length of your video using the trim filter—by specifying the duration or the start/end time of your desired output video.

    Specifying Duration

    ffmpeg -i input.mp4 -vf trim=duration=x output.mp4

    x - duration in seconds

    Specifying Start/End Time

    ffmpeg -i input.mp4 -vf trim=start=x:end=y,setpts=PTS-STARTPTS output.mp4

    x - the time of the start of the kept section in the seconds unit

    y - the time of the end of the kept section in the seconds unit

    🐻 Bear Tips: setpts=PTS-STARTPTS is added to the command to adjust the timestamps of the output video. This resets the timestamps of the output video to start from 00:00:00.

    Merging Videos

    Depending on their codes, there are a few approaches to merging videos using FFmpeg.

    If the videos have the same codec, you can use one of the options below:

    Option 1: Using the “concat” Demuxer

    First, create a .txt file and list the paths of the video files that you want to merge:


    file /Users/Video/input1.mp4
    file /Users/Video/input2.mp4

    Then, merge the videos using the “concat” demuxer by executing the command below:

    ffmpeg -f concat -safe 0 -i join_video.txt -c copy output.mp4

    Option 2: Using the “concat” Protocol (Video Formats that Support File-Level Concatenation Only)

    ffmpeg -i "concat:input1.ts|input2.ts" -c copy output.ts

    The concat protocol works only on video formats that support file-level concatenation like MPEG-2 TS. It cannot be used for other video formats like MP4 and WEBM.

    If the videos have different codecs, we can use the concat filter:

    ffmpeg -i input1.mp4 -i input2.wmv -filter_complex "[0:0][0:1][1:0][1:1]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4

    🐻 Bear Tips: You can use Bannerbear’s Movies API to easily merge videos programmatically, regardless of their codecs.

    Converting Video Formats

    To convert a video from one format to another, simply specify the original file name and the output file name in the desired format:

    ffmpeg -i [input_file_name.ex1] [output_file_name.ex2]

    Adding Watermarks

    The command below scales the watermark image to nx of the video’s height and overlays it on top of the video at position (x, y):

    ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1][0]scale2ref=oh*mdar:ih*n[logo][video];[video][logo]overlay=x:y" output_.mp4

    You can replace overlay=x:y with the options below to position it in different locations:

    Center: overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2

    Top Left: overlay

    Top Right: overlay=(main_w-overlay_w):0

    Bottom Left: overlay=0:(main_h-overlay_h)

    Bottom Right: overlay=(main_w-overlay_w):(main_h-overlay_h)

    🐻 Bear Tips: Using Bannerbear, you can add a watermark to videos in any position using a drag-and-drop interface. The size and transparency can be easily adjusted too!

    Adding Subtitles

    Using FFmpeg, you can add hard or soft subtitles to a video:

    Hard Subtitles (SRT Format)

    To add subtitles in the SRT format, we can use the subtitles filter:

    ffmpeg -i input.mp4 -vf subtitles=subtitle.srt output.mp4

    Hard Subtitles (ASS Format)

    For subtitles in the ASS format, we can use the ass filter:

    fmpeg -i input.mp4 -vf ass=subtitle.ass output.mp4

    Note: To use the ass filter, you need to have the libass library enabled at compile time.

    Soft Subtitles

    Soft subtitles allow you to add subtitles in multiple languages to your video. The command below adds two soft subtitles (English and Chinese) to the video:

    ffmpeg -i input.mp4 -i subtitle.en.srt -i subtitle.chi.srt -map 0 -map 1 -map 2 -c copy -c:s mov_text -metadata:s:s:0 language=eng -metadata:s:s:1 language=chi output.mp4

    You can modify the command above by adding another subtitle file (e.g., subtitle.jp.srt) along with an extra map option, -map 3.

    Extracting Audio

    You can extract the audio from a video using a command that is similar to converting the file format, but with an audio file extension for the output file:

    ffmpeg -i [input_file_name.ex1] [output_file_name.ex2]

    Here’s a list of common audio formats that are supported by FFmpeg:

    • MP3 (.mp3)
    • AAC (.m4a)
    • WAV (.wav)
    • OGG (.ogg)
    • FLAC (.flac)
    • WMA (.wma)

    Using Bannerbear API

    Besides FFmpeg, you can use Bannerbear's Video Generation API to edit video programmatically or add various editing features to your application.

    The API allows you to integrate video generation capabilities into your application by making a simple API request. With Bannerbear, you can merge videos with different formats, resize videos, add watermarks and subtitles, etc., all using a single API!

    Here’s an example of a video with subtitles added using Bannerbear:

    Full tutorial: How to Auto-Generate Subtitles/Closed Captions for Tiktok & Instagram

    🐻 Bear Tips: Bannerbear’s auto-subtitle feature transcribes videos and adds subtitles to them automatically, without needing a subtitle file (.srt/.ass).

    Compared to FFmpeg, editing videos programtially using Bannerbear is easy as it provides you a drag-and-drop interface to visualize the result. You can also easily automate the video editing process to generate videos based on a design template.

    If you're interested in exploring these features and seeing how you can edit or generate videos with Bannerbear, you can sign up for a new account for free! It's a great opportunity to test out the Video Generation API and see how it can benefit you.

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    How to Automatically Add Watermark to Instagram Reels For Reposting Using Bannerbear (Node.js Tutorial)

    Posting a mix of original and user-generated content, including properly credited reposts, can be an effective social media strategy for media companies to engage with their audience and keep their feed dynamic and interesting. Let's learn how to streamline this process using Bannerbear in Node.js!

    Ecommerce Localization: How to Generate Images with Local Currencies and Languages

    Localized content improves customer experiences while improving your business standing. Here's how to automatically localize pricing graphics with nocode tools.

    How to Automatically Add Personalized Images to Your Beehiiv Newsletter

    Learn how to enhance your Beehive newsletter with personalized images using Bannerbear. In this article, we will learn how to automatically add personalized images to Beehiiv newsletters to make the content more relevant and engaging for your subscribers.

    Automate & Scale
    Your Marketing

    Bannerbear helps you auto-generate social media visuals, banners and more with our API and nocode integrations

    FFmpeg Video Editing Essentials: Trimming, Merging, Subtitling, and More!
    FFmpeg Video Editing Essentials: Trimming, Merging, Subtitling, and More!