DEV Community

Cover image for Compress, Convert and Trim Videos with Command Line
Vladimir Vovk
Vladimir Vovk

Posted on • Edited on

Compress, Convert and Trim Videos with Command Line

ffmpeg

Often we have videos of different formats and resolutions from our phones and cameras, and also, from our friends and family. Sometimes these videos are uncompressed, so they take up much space.

There are many methods how we can solve this problem. But here, I would like to tell you about FFmpeg. It is a free and open-source tool available for all major operating systems. FFmpeg is fast and gives good results in compression, changing formats, and resolution.

To compress our favorite cats.mov video and convert it to mp4 format we need to run:



ffmpeg -i cats.mov cats.mp4


Enter fullscreen mode Exit fullscreen mode

Pretty easy, right? The only downside of this command is that ffmpeg does not give us user-friendly UI. There are no progress bar or estimated time to finish.

ffmpeg

menc

Introducing menc (media encoder) a small "wrapper" utility around ffmpeg which gives a nicer UI and can help to process several files more easily.

To compress the same cats.mov video with menc we need to run:



npx menc cats.mov


Enter fullscreen mode Exit fullscreen mode

menc

It will use the mp4 format by default and create a cats.mp4 file in the same folder.

Please ensure you have Node.js installed on your machine.

If you want to extract audio or change resolution it can be done with the --format argument. For example, the following command will compress our video and change the resolution to full HD.



npx menc -f fhd cats.mov


Enter fullscreen mode Exit fullscreen mode

You can find all supported formats with --help argument:



npx menc --help


Enter fullscreen mode Exit fullscreen mode

menc --help

To trim a media file we can use the --start-time and --end-time arguments:



npx menc -s 10 -e 1:09:04 cats.mov


Enter fullscreen mode Exit fullscreen mode

This command will compress the cats.mov file into cats.mp4 from the 10th second to 1:09:04.

If we want to trim a file without changing format (re-encoding) we can use the --copy argument:



npx menc -e 1:05 --copy cats.mp4


Enter fullscreen mode Exit fullscreen mode

This command will trim the cats.mp4 (no re-encoding) into a new file from the beginning until 1:05.

To convert several files we can use the wildcard character. For example, if we have more than one mov file we can process them all with only one command:



npx menc *.mov


Enter fullscreen mode Exit fullscreen mode

And if we want to write results into a separate my_compressed_mp4_files folder we can do that with the --dir argument:



npx menc -d my_compressed_mp4_files *.mov


Enter fullscreen mode Exit fullscreen mode

Please try menc, give it a star 🌟, and happy hacking! 💻

Credits

Photo by Alan Alves on Unsplash

Top comments (0)