Cắt Hoặc Cắt Một Phần Và Nối Files MP4 Dùng FFmpeg - cut and join a MP4 video

Cắt Hoặc Cắt Một Phần Và Nối Files MP4 Dùng FFmpeg - cut and join a MP4 video
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 ,
How can I crop a video with ffmpeg?

Use the crop filter:
ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4

Where the options are as follows:
  • out_w is the width of the output rectangle
  • out_h is the height of the output rectangle
  • x and y specify the top left corner of the output rectangle

Original image

original image
Original 320x240 image

Example 1

80x60
To crop a 80×60 section, starting from position (200, 100):
ffmpeg -i in.mp4 -filter:v "crop=80:60:200:100" -c:a copy out.mp4
  • The audio is stream copied in this example, so re-encoding is avoided.

Example 2

bottom right quarter
To crop the bottom right quarter:
ffmpeg -i in.mp4 -filter:v "crop=in_w/2:in_h/2:in_w/2:in_h/2" -c:a copy out.mp4
This is the same as:
ffmpeg -i in.mp4 -filter:v "crop=320/2:240/2:320/2:240/2" -c:a copy out.mp4
Which is the same as:
ffmpeg -i in.mp4 -filter:v "crop=240:120:240:120" -c:a copy out.mp4
  • You can refer to the input image size with in_w and in_h as shown in this first example. The output width and height can also be used with out_w and out_h.

Example 3

20 pixels from the top, and 20 from the bottom
Crop 20 pixels from the top, and 20 from the bottom:
 ffmpeg -i in.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy out.mp4
  • The filter will automatically center the crop if x and y are omitted such as in this example.

Previewing

You can take a crop (heh heh) and preview it live with ffplay:
ffplay -i input -vf "crop=in_w:in_h-40"
This way you can experiment and adjust your cropping without the need to encode, view, repeat.

Notes

  • Default encoder for MP4 is libx264 (H.264 video) or mpeg4 (MPEG-4 Part 2 video) depending on your ffmpeg build. See FFmpeg Wiki: H.264 Video Encoding Guide for more info.
  • Instead of cropping and re-encoding, consider cropping upon playback. This is possible with any player worth using.
  • Ancient ffmpeg builds used -croptop-cropbottom-cropleft-cropright options instead of the crop filter. If this is the case for you then get a modern ffmpeg. Development is very active and there is no reason to use an antique.

0 Nhận xét