Forem

abbazs
abbazs

Posted on

Essential FFmpeg Recipes for Video Manipulation

FFmpeg is a powerful command-line tool for handling multimedia data. Whether you're trimming, splitting, or editing videos, FFmpeg provides an efficient way to achieve your goals. In this article, we’ll explore common FFmpeg recipes for video manipulation.

Installing FFmpeg

Before diving into the recipes, ensure you have FFmpeg installed on your system. Here’s how you can install it on Ubuntu and Windows:

On Ubuntu:

  1. Update the package list:
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install FFmpeg:
   sudo apt install ffmpeg
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation:
   ffmpeg -version
Enter fullscreen mode Exit fullscreen mode

On Windows:

  1. Download the FFmpeg executable from the official website.
  2. Extract the downloaded ZIP file to a directory (e.g., C:\ffmpeg).
  3. Add FFmpeg to your system PATH:
    • Go to System Properties > Advanced > Environment Variables.
    • Under System Variables, select Path and click Edit.
    • Add the path to the FFmpeg bin directory (e.g., C:\ffmpeg\bin).
  4. Verify the installation by opening a command prompt and running:
   ffmpeg -version
Enter fullscreen mode Exit fullscreen mode

Now that you have FFmpeg installed, let’s explore some essential recipes for video manipulation.

1. Removing the First 5 Minutes

To remove the first 5 minutes of a video:

ffmpeg -i input.mp4 -ss 00:05:00 -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode
  • -ss 00:05:00: Skips the first 5 minutes.
  • -c copy: Copies the streams without re-encoding for speed.

2. Removing the Last 5 Minutes

To remove the last 5 minutes, you need to calculate the duration of the video and extract everything except the last 5 minutes:

ffmpeg -i input.mp4 -to $(($(ffmpeg -i input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , | cut -d: -f1)*3600 + $(ffmpeg -i input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , | cut -d: -f2)*60 + $(ffmpeg -i input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , | cut -d: -f3) - 300)) -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode

Here, 300 represents the last 5 minutes in seconds.

3. Copying the First 5 Minutes

To extract only the first 5 minutes of a video:

ffmpeg -i input.mp4 -t 00:05:00 -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode
  • -t 00:05:00: Specifies the duration to copy.

4. Copying the Last 5 Minutes

To copy the last 5 minutes of a video, calculate the starting time:

ffmpeg -i input.mp4 -ss <start_time> -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode

Replace <start_time> with the total duration minus 5 minutes. Use the following command to calculate the duration:

ffmpeg -i input.mp4
Enter fullscreen mode Exit fullscreen mode

5. Removing a Section Between 5 Minutes and 8 Minutes

To remove a specific section of the video:

  1. Extract the part before 5 minutes:
ffmpeg -i input.mp4 -to 00:05:00 -c copy part1.mp4
Enter fullscreen mode Exit fullscreen mode
  1. Extract the part after 8 minutes:
ffmpeg -i input.mp4 -ss 00:08:00 -c copy part2.mp4
Enter fullscreen mode Exit fullscreen mode
  1. Concatenate the parts:

Create a text file (file_list.txt) with the following content:

file 'part1.mp4'
file 'part2.mp4'
Enter fullscreen mode Exit fullscreen mode

Run:

ffmpeg -f concat -safe 0 -i file_list.txt -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode

6. Copying Video Between 5 Minutes and 8 Minutes

To copy only a specific section:

ffmpeg -i input.mp4 -ss 00:05:00 -to 00:08:00 -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode

7. Splitting a Large Video into 20MB Chunks

To split a video into chunks of approximately 20MB:

ffmpeg -i input.mp4 -f segment -segment_size 20M -c copy output%03d.mp4
Enter fullscreen mode Exit fullscreen mode
  • -segment_size 20M: Specifies the maximum size of each segment.

8. Splitting a Large Video into 5-Minute Chunks

To split a video into 5-minute segments:

ffmpeg -i input.mp4 -f segment -segment_time 300 -c copy output%03d.mp4
Enter fullscreen mode Exit fullscreen mode
  • -segment_time 300: Specifies the duration of each segment in seconds (5 minutes = 300 seconds).

Final Thoughts

These FFmpeg recipes provide a foundation for common video editing tasks. By combining options and filters, you can customize these commands for more advanced use cases. Explore the FFmpeg documentation to dive deeper into its capabilities.

Happy editing!

Billboard image

Synthetic monitoring. Built for developers.

Join Vercel, Render, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay