Linux: Cutting or Merging Video Clips in Bash

in linux •  2 months ago 

When I record I generally do 1 large video then cut 1 section to post onto social media. Instead of loading a video editor (like kdenlive) I just open a terminal in the folder, load the video into VLC to determine start point and duration for the cut I want, then run the below Bash command.

[1] Cutting Clips

  • ffmpeg -ss -i original.mp4 -to cut.mp4
  • example: ffmpeg -ss 00:03:02 -i original.mp4 -to 0:33 cut01.mp4

Very rarely do I use 2 cuts, but when I do I use the Bash command below
[2] Merging 2 Clips, both clips need to be cut from same original video

  • mencoder cut01.mp4 cut02.mp4 -ovc copy -oac pcm -o merged.mp4

Original Video (low quality, back then I had a cheap Rollei action camera)

  • WainuiSummit.mp4 (51min44secs 2.7GB 960x540 30fps H264)

Play the video, select your start points and duration for both cuts

  • ffmpeg -ss 01:12 -i WainuiSummit.mp4 -to 00:34 cut04.mp4
  • ffmpeg -ss 02:18 -i WainuiSummit.mp4 -to 02:08 cut05.mp4

play each cut and adjust timings until satisfied
type in terminal -> ls to display files in the current folder

Merging

  • mencoder cut04.mp4 cut05.mp4 -ovc copy -oac pcm -o merged02.mp4 (2mins42secs 129MB)

Note
If either ffmpeg or mencoder doesn't work, just type each of those programs into the terminal on separate lines, Linux will automatically give your instructions on how to install them for you (this is another reason why I love Linux).

Other commands that can be used here
[3] Remove sound from the video

  • ffmpeg -i video.mp4 -vcodec copy -an just_videonosound.mp4
    [4] Adding background music to a video (I never do this but it's shown here)
    you must trim the sound to the exact length of the video
    this replaces the original audio track
  • mencoder video.mp4 -o videowithmusic.mp4 -ovc copy -oac copy -audiofile audio.mp3

I did create a video with 2 cuts, a merge with sound removal to make sure these commands still work but I didn't see any point on uploading it.


Originally posted on Staying Active Body and Mind. Steem blog powered by ENGRAVE.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!