How to cut and join a MP4 video in four steps using ffmpeg
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!
And with the following four steps it is possible to remove the useless pieces of a MP4 video:
- 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.
- 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. - 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
- 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.
- 1Thanks to a comment on google+, I have just discovered that even Avidemux can run commands on a Terminal: look here.
- 2FFmpeg 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.
- 3Source: Encode/H.264 – FFmpeg
- 4Source: Concatenate – FFmpeg
- 5A 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 ,
Add new comment