How to cut and join a MP4 video in four steps using ffmpeg

Submitted by Nicola Rainiero on 2015-04-19 (last updated on 2016-08-11)

How many time we have to fix a video removing the parts that we don’t need? There are a lot of video editing programs that solve this elementary issue, including great open source ones,  but generally they require to process the video and audio streams and so a lot of resources. FFmpeg and the command-line can cut and join the video in four steps copying the same video and audio codec even on a MP4 video encoded in h.264 and AAC.

Internet gives us daily many contents, with an increasing amount of video sources: podcasts, video-sharing websites, lectures and so on. At the same time we might have the necessity to store some of them in ours local stores. Or just a little part of them if they contain useless pieces.

There are many softwares that can help us to download the videos on Internet (if the website or browser add-on doesn't allow it), for example 4K Video Downloader and to edit them like many open source video editing programs like Avidemux1 or VirtualDub.

Although today our computers are enough powerful to process a video stream and our local storages have a lot of free gigabytes to store them, I think in many cases we don’t need video editing software to cut and join videos: FFmpeg2 is enough!

How to cut and join a MP4 video in four steps using ffmpeg

And with the following four steps it is possible to remove the useless pieces of a MP4 video:

  1. Find the start and finish timestamp of the element to preserve. I use my favorite video player: mplayer with the "o" key (Toggle OSD states: none / seek / seek + timer / seek + timer + total time). Of course you can use any ones, just remember that the format must be saved in HH:MM:SS.S. For example 01:34:50.1 means 1 hour, 34 minutes and 50.1 seconds.
  2. Execute a FFmpeg command-line with the timestamp for every selection. The choice of best encoding process is complicated and strictly connected to the users needs. I can only put some suggestions like this3:
    ffmpeg -i originalVideo.mp4 -ss 01:34:50 -to 02:22:50 -c:v libx264 -preset ultrafast -qp 0 -c:a libmp3lame -b:a 160k -ac 2 -ar 44100 newStream1.mp4
    Another solution that save time and storage resources is4:
    ffmpeg -i originalVideo.mp4 -ss 01:34:50 -to 02:22:50 -codec:v copy -codec:a copy newStream1.mp4
    In the second case I make a simple copy of the audio and video codecs. Generally it works with some of them, but if you video is encoded in h.264 and AAC5, it is necessary the third step. Otherwise you can go directly to the fourth.
  3. If you have MP4 files and you have only copied the codecs, these will probably be choppy and out of sync. So you have to transcoding them to mpeg transport streams before concatenating them:
    ffmpeg -i newStream1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
  4. Concatenate the media files:
    ffmpeg -i "concat:newStream1.mp4newStream2.mp4|newStream[...].mp4|newStream[n-part].mp4" -c copy finalVideo.mp4
    or if you have transcoded them:
    ffmpeg -i "concat:intermediate1.ts|intermediate2.ts|intermediate[...].ts|intermediate[n-part].ts" -c copy -bsf:a aac_adtstoasc finalVideo.mp4

Check the finalVideo.mp4 with your video player! You can play it selecting only the part that you have previously joined. Using mplayer:

mplayer -ss 00:17:30 -endpos 10 finalVideo.mp4

Where:

  • -ss 00:17:30 means the starting point;
  • -endpos 10 means that after 10 seconds the player exits.
  • 1. Thanks to a comment on google+, I have just discovered that even Avidemux can run commands on a Terminal: look here.
  • 2. FFmpeg is a free software project that produces libraries and programs for handling multimedia data. FFmpeg includes libavcodec, an audio/video codec library used by several other projects, libavformat, an audio/video container mux and demux library, and the ffmpeg command line program for transcoding multimedia files. FFmpeg is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+ (depending on which options are enabled). Source: FFmeg on Wikipedia.
    To install FFmpeg in Ubuntu/Linux Mint open Terminal and execute the following commands:
    sudo add-apt-repository ppa:jon-severinsson/ffmpeg
    sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
    sudo apt-get update
    sudo apt-get install ffmpeg

    I use also the ppa:ffmulticonverter/stable repository to install FF Multi Converter in my system, it is a simple graphical application which enables you to convert audio, video, image and document files between all popular formats, using and combining other programs. It uses ffmpeg for audio/video files, unoconv for document files and ImageMagick for image file conversions.

  • 3. Source: Encode/H.264 – FFmpeg
  • 4. Source: Concatenate – FFmpeg
  • 5. A fast way to find the codecs through the terminal could be:
    ffmpeg -i originalVideo.mp4 2>&1 | grep Video: | awk '{print $3,$4}' | tr -d ,
    ffmpeg -i originalVideo.mp4 2>&1 | grep Audio: | awk '{print $3,$4}' | tr -d ,
    



Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.

Comments

Mike (not verified)

Thu, 10/22/2015 - 00:32

Hi Nicola,
thank you for publishing your tips for cutting & joining MP4s with ffmpeg.
I Currently run different converters (freeware and trial software) to see what can fit my cases. I also work with VirtualDub and FFMpeg where after editing I join my videos in a similar fashion as you describe. I'm using a batch file with this content:

ffmpeg -i video1.mp4 -c copy -bsf h264_mp4toannexb temp1.ts
ffmpeg -i video2.mp4 -c copy -bsf h264_mp4toannexb temp2.ts
ffmpeg -i video3.mp4 -c copy -bsf h264_mp4toannexb temp3.ts
ffmpeg -i video4.mp4 -c copy -bsf h264_mp4toannexb temp4.ts
ffmpeg -i video5.mp4 -c copy -bsf h264_mp4toannexb temp5.ts
ffmpeg -i "concat:temp1.ts|temp2.ts|temp3.ts|temp4.ts|temp5.ts" -c copy -bsf aac_adtstoasc output.mp4
del temp1.ts temp2.ts temp3.ts temp4.ts temp5.ts

I understand that my videos must all have the same parameters (codecs, size, bitrate, audio data...) in order to use this batch, otherwise I would have to re-encode these videos in a single one. The big advantage with this batch is: with similar videos you don't have to re-encode and this will save you a lot of system ressources.

BUT:
If I look in the Command Line Window when executing this batch file, I notice a lot of yellow error messages telling that theere 'could be mismatches in the timestamp'. My videos play fine however. What does this mean, can I just ignore these messages?
I'm using the latest ffmpeg: v. N-76137.

Thanks very much if you could clarify!
Greets,
Mike

Anonimo (not verified)

Sun, 11/13/2016 - 20:10

i did this

ffmpeg -i originalVideo.mp4 -ss 01:34:50 -to 02:22:50 -codec:v copy -codec:a copy newStream1.mp4

but it just do it for video first audio and sub in the video and doesn't include the remaining track
eg.if my video file have 1.video 2.engAudio 3.japAudio 4.signs&Songs 5.engSub and so on

can u help he in triming the whole video and not few track in video

thanks

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.