Thursday, March 16, 2017

Trimming videos via FFMPEG

After continuous insisting from my friends, I have recently set up my Steam account and purchased two games, Portal and Portal 2. And I have also been recording the screen and audio while playing just for archival and other purposes such as uploading them to YouTube and profit from videos that get high hits.

Portal 2, for example has many chapters and in each chapter you have a singnificant set of levels to finish to levels. Chapter 1 of Portal 2 has 19 levels. Nonetheless, after recording one hour session, I had to split them later.

I found intially an excellent trimmmer that is really useful in QuickTime: 

However, the process was incredibly time-consuming. So, I thought I want to do it via coding and it was easier than I imagined.

Indeed, the first library that came was FFmpeg: the revolutionary and prominent and useful and capabable video editing software on existence.

FFmpeg is way more capable than just trimming videos, so trimming via FFmpeg was very easy. Searching through their official documentation, I was able to find this link.

In short the ffmpeg command needed for trimming is this:
ffmpeg -i input.mp4 -ss 00:01:40 -to 00:02:12 -c copy output.mp4

I have tested it tons of times and found out it is the fastest and safest way to trim videos. Here are the parameters in short: 
  • -i: This specifies the input file. In that case, it is (input.mp4).
  • input.mp4: This your input file. You can name it as anything you want.
  • --s: Used with -i, this seeks in the input file (input.mp4) to position. 
  • 00:01:00: This is the time your trimmed video will start with.
  • -to: This specifies duration from start (00:01:40) to end (00:02:12).
  • 00:02:00: This is the time your trimmed video will start with.
  • -c copy: This is an option to trim via stream copy.
  • output.mp4: This your output file. You can name it as anything you want.
You can run the ffmpeg command on any bash terminal and it will run smoothly. Nonetheless, if you want to trim around 13 videos you'd have to write the command 13 times, which left me annoyed. So, I had to write a small python script take care of it.

You can run this code by simply calling python and indeed you have to change the arguments.