How to Crop and Resize Videos Using FFmpeg

Master video cropping and resizing with FFmpeg in this developer-friendly guide. Learn essential FFmpeg cropping and resizing commands, and how to automate them using scripts.
by Josephine Loo ·

Contents

    If you're a developer building a video editing tool or automating video workflows, chances are you’ve come across FFmpeg before. It can look a bit intimidating at first, especially if you’re not used to command line tools, but once you get the hang of it, it’s an incredibly powerful tool for working with media files.

    Knowing how to crop and resize videos with FFmpeg is a must if you're building features like smart cropping, template-based editing, or converting dimensions for different social media platforms. In this guide, I’ll walk you through the commands to crop and resize videos using FFmpeg. You’ll learn how to crop specific areas, resize to fit different platforms, and even automate the whole process.

    Whether you're writing an automation script, integrating FFmpeg into your app, or just exploring how it works, this article will cover the essential commands you need for cropping and resizing videos.

    What is FFmpeg

    FFmpeg is a powerful cross-platform command-line tool used for handling video and audio files. It can decode, encode, transcode, mux, demux, stream, filter, and play media in just about any format, and it works on all major platforms, including Linux, macOS, Windows, and more.

    FFmpeg comes with a set of tools that let you convert, play, and analyse media files, as well as libraries for developers to use in various applications. These include:

    • libavcodec
    • libavutil
    • libavformat
    • libavfilter
    • libavdevice
    • libswscale
    • libswresample.

    🐱 Meow Memo: These libraries come bundled with FFmpeg, there's no need to install them separately.

    How to Install FFmpeg

    Before you begin, make sure to download FFmpeg from the official website. You can follow this step-by-step guide to install FFmpeg on your Mac, Windows, or Ubuntu/Linux.

    Once it’s installed, you can run the following command in your terminal/command prompt to verify the installation:

    ffmpeg -version
    

    Cropping and Resizing Basics: What Do the FFmpeg Options Mean

    Before we jump into the commands, let’s take a quick look at some of the FFmpeg flags and filters you’ll encounter when cropping and resizing videos:

    • i: Specifies the input video file.
    • vf: Short for video filter, this flag is used to apply transformation filters like crop, scale, or cropdetect.
    • crop=w:h:x:y: Crops the video to a width w and height h, starting from coordinates (x, y)(top-left origin).
    • scale=w:h: Resizes the video to width w and height h. Use 1 for automatic scaling to maintain aspect ratio.
    • cropdetect: Automatically detects the area with content in a video (usually used to remove black borders).
    • in_w / in_h: Dynamic variables that represent the input video’s width and height. Helpful when you don’t want to hardcode dimensions.
    • ffprobe: A separate tool that comes with FFmpeg, used to inspect media files (like checking a video’s width and height).

    Understanding these options will make it much easier to tweak the commands to suit your own use case, especially if you're integrating FFmpeg into a backend service or part of a video processing workflow.

    Cropping vs. Resizing: What’s the Difference

    Cropping and resizing are two different ways to change the video dimensions. Each method has its own specific use case, so it’s important to know when to use which. Here’s a simple explanation of both:

    Cropping

    Cropping is about removing unwanted portions of the video from its edges. Essentially, it involves selecting a specific part of the video and discarding the rest. This method is useful when you only need a particular section of the video or when you want to remove things like black bars, logos, or any other unnecessary elements from the original footage.

    Resizing

    Resizing, or scaling, adjusts the video’s dimensions to match a specific resolution or aspect ratio. This involves either stretching or compressing the entire video to fit a particular screen size or to make it compatible with specific platforms such as YouTube, TikTok, or Instagram.

    Checking Original Video Dimensions

    Before cropping or resizing, it’s useful to check the input video’s dimensions. You can do this with ffprobe:

    ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4
    

    Running the command above will return you the video’s dimensions:

    a screenshot of checking a video's original dimension using ffprobe.png

    🐱 Meow Memo: Knowing this information before cropping or resizing your videos can help avoid unintended distortions or black bars.

    How to Crop Videos Using FFmpeg

    Cropping lets you cut out a portion of the video. It is useful for zooming in on a subject, removing unwanted borders, or adjusting frame composition. To crop videos using FFmpeg, we will use the "crop" filter:

    Basic Command

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

    Let’s look at some examples…

    Example 1: Center Crop

    The command below crops the center portion of the video to dimensions 1280x720. When x and y are not specified, FFmpeg will auto-center the crop area:

    ffmpeg -i input.mp4 -vf "crop=1280:720" output.mp4
    

    🐱 Meow Memo: When cropping a video, the new dimensions must always be smaller than the original dimensions.

    Example 2: Crop from Specific Coordinates

    The command below crops the video to 640x480, starting from 100 pixels from the left and 50 pixels from the top. Specifying the coordinates is useful when you know exactly where the subject is positioned:

    ffmpeg -i input.mp4 -vf "crop=640:480:100:50" output.mp4
    

    Example 3: Dynamic Crop

    By using in_w and in_h, you can dynamically access the width and height of the input video. For example, the command below crops the center half of the video, and it will work the same way, no matter the original dimensions of the input video:

    ffmpeg -i input.mp4 -vf "crop=in_w/2:in_h/2" output.mp4
    

    Trimming Black Borders with ‘cropdetect’

    Black borders are very common in videos, especially when trying to fit them into a specific aspect ratio without stretching or distorting the content. If you want to remove these borders, FFmpeg has a handy filter called cropdetect.

    The filter will analyse the video, detect the area without black borders, and then calculate and display the suggested crop size and position for you:

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

    Running the command will return the crop size and position:

    a screenshot of the crop size and posittion from using the cropdetect filter.png

    Use those values in your regular crop command to remove the black borders from the video:

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

    How to Resize a Video with FFmpeg

    Resizing is often needed when preparing videos for different screen sizes or platforms (e.g., YouTube Shorts, Instagram Reels, web thumbnails). Here’s how to do it with FFmpeg:

    Basic Command

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

    Example 1: Basic Resize

    The command below sets the video to a new dimension—1200x720, regardless of its original aspect ratio:

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

    Example 2: Maintain Aspect Ratio (Set Width Only)

    To adjust the width but keep the original aspect ratio, you can use the command below. FFmpeg will automatically calculate the height based on the original aspect ratio:

    ffmpeg -i input.mp4 -vf "scale=1200:-1" output.mp4
    

    Example 3: Maintain Aspect Ratio (Set Height Only)

    To set the height and maintain the aspect ratio, do it the other way round:

    ffmpeg -i input.mp4 -vf "scale=-1:720" output.mp4
    

    🐱 Meow Memo: You can crop and resize a video in one go using a single command. For example, ffmpeg -i input.mp4 -vf "crop=1280:720,scale=640:360" output.mp4 first crops the video to 1280x720, then scales it down to 640x360.

    Bonus: Automating Cropping and Resizing with a Script

    If you’re working with multiple video files, say, dozens or even hundreds of them, it’s not practical to run FFmpeg commands manually on each one. Instead, you can automate the process with a script.

    Here’s a simple shell script that crops and resizes every .mp4 in the directory and stores the result in an output/ folder:

    for f in *.mp4; do
      ffmpeg -i "$f" -vf "crop=1280:720,scale=640:360" "output/$f"
    done
    

    This script allows you to batch-process all the videos in a folder, which can be very helpful for automation workflows, content preprocessing, or repurposing videos across different platforms.

    🐱 Meow Memo: If you’re using Node.js, Python, or another backend language, you can also spawn FFmpeg as a child process from your script and loop through input files that way.

    Automating Video Editing with Templates and APIs

    While FFmpeg gives you powerful control over cropping, resizing, and other media manipulation functions, it might not be the most practical choice if your goal is to let users generate dynamic videos with custom content like names, quotes, or uploaded images. Instead of building everything from scratch with FFmpeg, try using Clipcat.

    Clipcat - API for Advanced Automated Video Generation.png

    Clipcat is a video generation tool that lets you create videos programmatically using templates. You only need to design the video template once, then use its API to dynamically update elements like text (e.g., titles, captions, quotes), images (e.g, profile pictures, logos, product photos), and videos (e.g., user-uploaded clips or animated backgrounds).

    This makes it perfect for use cases like:

    • Auto-generating promotional videos for new products
    • Creating personalized thank-you or testimonial videos
    • Generating UGC-style content at scale for social media

    Here are some examples of the video templates:

    Template 1

    Template 2

    Template 3

    It’s a faster and more efficient way to create personalised videos without manually editing clips. The best part is, you can easily integrate it into your application or workflow using the API!

    🐱 Meow Memo: Want to try Clipcat for your video projects? Sign up now and get 100 free API credits to get started!

    Conclusion

    Learning how to crop and resize videos with FFmpeg is a must if you're building anything that involves media processing, whether it’s for building a video editing tool, automating video workflows, or simply adjusting videos for various platforms.

    By understanding these core commands, you’ll be able to work more efficiently when you combine them with automation scripts or integrate them into your backend. And if you're looking to enable video generation using templates and automation, tools like Clipcat can help and really save you time, since you don’t have to build everything from scratch!

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.
    How to Crop and Resize Videos Using FFmpeg
    How to Crop and Resize Videos Using FFmpeg