Mike Slinn

MediaTrim: A Friendly Interface To FFmpeg

Published 2022-01-23. Last modified 2023-11-28.
Time to read: 3 minutes.

This page is part of the av_studio collection.

When I set up a 4K camera on location and let it roll while I play a set, I get massive video files. I also get large files when using OBS Studio; my usual setup records video from Cam Link 4K and audio using RME TotalMix. I need to be able to trim video files so the portions before and after the good stuff are discarded.

Many online conversations revolve around trimming video files. Dozens of PC and Mac programs exist to do that task, mostly of low quality and/or bothersome to use. Other solutions are overkill, for example, Adobe Premiere Pro and DaVinci Resolve.

Introducing MediaTrim

In this article, I present a command-line program, called MediaTrim, that can dramatically reduce file size while preserving quality and properly trimming files to specified time periods.

The program is published as a Ruby gem. Installing the gem also installs a command called trim. No programming knowledge is required to use it.

Mediatrim:

  • Is a F/OSS Ruby gem that provides a new command that you can use in a console session and in a script.
  • Includes a Ruby API for use by other programs.
  • Works on Windows, Mac and Linux.

What Is FFmpeg?

ffmpeg is a powerful command-line media converter. It is not particularly user-friendly, but it works very well if you put in the time to learn it. The program described in this article provides an easy means of running ffmpeg for those who want to trim media.

FFmpeg is a universal media converter. It can read a wide variety of inputs – including live grabbing and recording devices – filter them, and transcode them into a plethora of output formats.

FFmpeg reads from an arbitrary number of input “files” (which can be regular files, pipes, network streams, grabbing devices, etc.), specified by the ‑i option, and writes to an arbitrary number of output “files”, which are specified by a plain output URL. Anything found on the command line that cannot be interpreted as an option is considered an output URL.

Each input or output URL can, in principle, contain any number of streams of different types (video, audio, subtitle, attachment, or data). The allowed number and/or types of streams may be limited by the container format. Selecting which streams from which inputs will go into which output is either done automatically or with the ‑map option (see the Stream selection chapter).

DJV

Need a way to figure out the start and stop times to trim a video? Djv is an excellent video player for this purpose. It is one of the very few video players that can step through a video frame-by-frame, forwards and backwards.

  • Mac, Windows, and Linux
  • Allows frame-by-frame stepping
  • Displays the current time reliably
  • F/OSS
  • High quality

Installing the Trim Command

You need a working Ruby environment to install this program. I describe how to set that up here.

The trim command is provided by the media_trim Ruby gem. Install it like this:

Shell
$ gem install media_trim

Help Message

The help message is:

Shell
$ trim -h
media_trim - Trim an audio or video file using ffmpeg
- Works with all formats supported by ffmpeg. - Seeks to the nearest frame positions by re-encoding the media. - Reduces file size produced by OBS Studio by over 80 percent. - Can be used as a Ruby gem. - Installs the 'trim' command.
When run as a command, output files are named by adding a 'trim.' prefix to the media file name, e.g. 'dir/trim.file.ext'. By default, the trim command does not overwrite pre-existing output files. When trimming is complete, the trim command displays the trimmed file, unless the -q option is specified
Command-line Usage: trim [OPTIONS] dir/file.ext start [[to|for] end]
- The start and end timecodes have the format [HH:[MM:]]SS[.XXX] Note that decimal seconds may be specified, and frames may not; this is consistent with how ffmpeg parses timecodes. - end defaults to the end of audio/video file
OPTIONS are: -d Enable debug output -f Overwrite output file if present -v Verbose output -V Do not view the trimmed file when complete.
Examples: # Crop demo/demo.mp4 from 15.0 seconds to the end of the video, save to demo/trim.demo.mp4: trim demo/demo.mp4 15
# Crop dir/file.mkv from 3 minutes, 25 seconds to 9 minutes, 35 seconds, save to demo/trim.demo.mp4: trim demo/demo.mp4 3:25 9:35
# Same as the previous example, using optional 'to' syntax: trim demo/demo.mp4 3:25 to 9:35
# Save as the previous example, but specify the duration instead of the end time by using the for keyword: trim demo/demo.mp4 3:25 for 6:10
Need a way to figure out the start and stop times to trim a video? DJV is an excellent video viewer https://darbyjohnston.github.io/DJV/ - allows frame-by-frame stepping - displays the current time reliabily - F/OSS - Mac, Windows, Linux - High quality

Example Usage

The following is a sample usage of trim, which extracts the portion of VideoFile.mkv from 00:00:25.000 to 00:02:52.000, and writes the extracted portion to a new file called VideoFile.trim.mkv:

Shell
$ trim VideoFile.mkv 25 2:52
Trimming '/mnt/f/work/VideoFile.mp4' from 00:25 to 2:52 (duration 2:27)
Trim took 0:03 

Now we have these files:

Shell
$ ls -AlF
total 1546136
-rw-r--r-- 1 mslinn mslinn   30579665 Jan 23 13:03 'trim.VideoFile.mkv'
-rwxrwxrwx 1 mslinn mslinn  180988555 Jan 22 18:31 'VideoFile.mkv' 

Next: Technical Implementation

The next article, MediaTrim Ruby Gem Implementation, discusses the technical implementation of this application.



* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.