DEV Community

Cover image for ffmpeg: Using Command Line To Cut Out Media Files
nabbisen
nabbisen

Posted on

ffmpeg: Using Command Line To Cut Out Media Files

* The cover image is originally by Greyerbaby and edited with great appreciation.

Summary

I like using command line tools when I deal with simple tasks, because they:

  • run quickly
  • need less memory
  • work well with target arrays
  • enable us to record the orders as text

Well, it's been much easier recently to host video files and show them on websites.
With ffmpeg (and also ffprobe), it's possible to cut them out and adjust their duration before uploading them.

Environment

  • ffmpeg / ffprobe: 4.1
✿ ✿ ✿

How To Use

Cutting Out Media Files
$ ffmpeg -ss %from-seconds% -t %to-seconds% -c copy -i %input.file% %output.file%

For example, ffmpeg -ss 0 -t 17 -i some-in.mp4 some-out.mp4 makes some-out.mp4 whose content is 0-17s of some-in.mp4.

Besides, -c is short for -codec, and -c copy means all the streams will be copied without reencoding.
It's used to escape from deteriorating them.
The manual of main options is here.

As to syntax of time duration, both of specified seconds and HH:MM:SS.m format are allowed.

$ ffmpeg -ss %from-HH:MM:SS.m% -t %to-HH:MM:SS.m% -c copy -i %input.file% %output.file%

For example, ffmpeg -ss 00:00:00.0 -t 00:00:17.0 -i some-in.mp4 some-out.mp4 is the same to the former example just using HH:MM:SS.m format.

Getting The Duration

ffprobe provides ways to get duration of media files.

In order to get number of seconds, the command is like this:

$ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 %input.file%
17.017000

In order to get HH:MM:SS.m, add sexagesimal option to the above command:

$ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %input.file%
0:00:17.017000

The manual of generic options is here.

✿ ✿ ✿

Happy terminaling 🌈

Top comments (0)