How to Cut and Save a Video Using a Simple Bash Script with ffmpeg


Video editing can be intimidating, especially if you aren't familiar with professional editing software. But what if you want to do something as simple as trimming a video to remove some unwanted sections? Thankfully, ffmpeg, a powerful multimedia processing tool, makes it incredibly easy—and you can do it with just a few lines of code in a Bash script!

In this post, we'll walk through how to use ffmpeg in a Bash script to trim the first 10:25 minutes from a video file, saving you the trouble of editing through a GUI.

What is ffmpeg?

ffmpeg is a free, open-source command-line tool that can handle video, audio, and other multimedia files. It supports almost all media file types and can perform a wide range of operations like converting formats, cutting videos, merging files, extracting audio, and much more.

Why Use a Bash Script for Trimming Videos?

While you can execute ffmpeg commands directly in the terminal, putting those commands into a script offers a few advantages:

  • Automation: Easily repeat the process for different videos.
  • Reusability: Save the script for future use, just by changing the input/output file names.
  • Convenience: No need to remember the exact ffmpeg syntax every time.

Step-by-Step Guide to Trimming a Video

In this tutorial, we'll focus on removing the first 10 minutes and 25 seconds of a video file called 3rdClass.mp4. We will save the remaining portion as a new file, 3rdClass_trimmed.mp4.

Prerequisites

Before we get started, you’ll need ffmpeg installed on your system. Here’s how to install it on Ubuntu or other Debian-based systems:

sudo apt-get install ffmpeg

For macOS users, you can install ffmpeg using Homebrew:

brew install ffmpeg

The Bash Script

Here’s the simple Bash script that will take care of trimming the first part of your video:

#!/bin/bash

# Input video file
input_video="3rdClass.mp4"

# Output video file
output_video="3rdClass_trimmed.mp4"

# Time to cut (10 minutes and 25 seconds)
start_time="00:10:25"

# Use ffmpeg to cut the video from the start_time until the end
ffmpeg -ss $start_time -i "$input_video" -c copy "$output_video"

echo "Video has been trimmed and saved as $output_video"

Let’s break down what each part of this script does:

  • input_video="3rdClass.mp4": This is the video file you want to trim. Replace this with the path to your video file.
  • output_video="3rdClass_trimmed.mp4": The trimmed video will be saved with this name.
  • start_time="00:10:25": This is the time at which we want to start saving the video. Everything before this timestamp will be removed.
  • ffmpeg -ss $start_time -i "$input_video" -c copy "$output_video":
    • -ss $start_time: Seeks to the specified timestamp (00:10:25).
    • -i "$input_video": Specifies the input video.
    • -c copy: Copies the video and audio streams without re-encoding them, making the process faster.

Running the Script

Here’s how to use the script:

  1. Save the script in a file, say trim_video.sh.

  2. Open a terminal and give the script executable permission using the following command:

    chmod +x trim_video.sh

  3. Run the script:

    ./trim_video.sh

After running the script, the trimmed video will be saved as 3rdClass_trimmed.mp4 in the same directory.

Why Use ffmpeg for Video Trimming?

  • Speed: By using the -c copy option, the script trims the video without re-encoding it, making the process extremely fast.
  • Simplicity: No need for a bulky video editing application. This script gets the job done quickly and effectively.
  • Flexibility: You can easily modify this script to cut from a different point or to remove a specific part of the video.

Advanced Usage: Cutting a Specific Segment

If you want to remove only a specific part of the video, say the segment from 00:10:25 to 00:15:00, you can modify the script like this:

ffmpeg -ss 00:10:25 -to 00:15:00 -i "$input_video" -c copy "$output_video"

This command will extract the portion of the video between 10:25 and 15:00 and save it to the output file.

Conclusion

In just a few steps, you've learned how to cut a video using a simple Bash script and ffmpeg. This powerful tool allows you to automate video editing tasks with ease, saving you time and effort.

The possibilities don’t stop here—ffmpeg can be used for much more than just trimming videos. You can convert video formats, extract audio, resize videos, and even create GIFs. It’s a fantastic utility to have in your toolbelt if you work with multimedia content.


0% Positive Review (0 Comments)