Skip to main content

How to cut unwanted video parts and join the rest ones into one video using FFmpeg?


I have read about cutting and splicing using concat but this is not enough for me.


I would like to cut marked video parts and join into single video file without re-encoding. Is it possible to cut and merge (in memory) in one go?


There should be no theoretical limit on the number of video parts to cut or join. Audio should be in sync.


Video: H.264+AAC


Edit:


From the answers I learned that video re-encoding is still needed.


I would like to clarify that cutting video clips from the middle of a video file might be done in multiple steps. I was thinking that processing the video in one go would be performance wise by saving some I/O activity. However it is not an objective in itself.



Answer



Your question, if I got got it right, has four main parts:



  • How to cut a time-segment from a movie

  • How to merge several segments together

  • How to do the above without transcoding the movie, hence not losing any quality

  • How to do the above efficiently in terms of speed



You can cut a segment by skipping to its starting point with the -ss option, then either setting the duration of the segment with -t, or setting the endpoint with -to:


# skip 30 seconds, then copy the next 60 seconds
ffmpeg -ss 30 -t 60 full-movie.mp4 segment.mp4

In the ffmpeg documentation please consult the section 5.4 Main Options regarding the difference between placing the -ss and -t options before the input file or before the output file. Once you experiment with this you may find this difference as relevant in your case.


Important note: The above example causes the video to be transcoded. We'll discuss below the possibility of doing this with no transcoding.



There are three primary methods to perform a merge of movies, two of them are best explained in this ffmpeg wiki article, and the third is the concat demuxer. If all your segments are identical in terms of a/v encoding spec and container format, you'll probably find the easiest method to be the concat: protocol:


# merge the segments
ffmpeg -i "concat:seg1.mp4|seg2.mp4|seg3.mp4" final.mp4

It is important to note that this merger, like the cutting, once again transcodes the movie, so we have here a transcode of a transcode, potentially degrading the video quality, so we really want to avoid this transcoding as much as we can!



The reason that the examples in the previous sections require transcoding is because each file and each resulting segment is a standalone movie that was packed as to provide all the stuff you expect when you open a full movie, such as the duration of the movie and other metadata. But hey, you're not looking to play these as standalone movies, you want these segments to become part of a larger video stream. So instead of serving ffmpeg with boxed movies, you should better re-package - re-mux - the input files into a real-time streaming container, forgetting about header stuff you don't really need at this point. In the case of H.264 the streaming mux format is called MPEG-TS, and here is how you re-mux your stream without transcoding it:


# re-muxing the whole movie (see a better option in next example)
ffmpeg -i full-movie.mp4 -c:v copy -c:a copy -bsf:v h264_mp4toannexb full-movies-as-ts.ts

Well, since you're already at it, you might as well use this opportunity to cut just the segment you need:


# skip 30 seconds and re-mux a 60 seconds segment
ffmpeg -ss 30 -t 60 -i full-movie.mp4 -c:v copy -c:a copy -bsf:v h264_mp4toannexb segment.ts

When you merge the TS segments you can also re-mux back to an mp4 container:


# merge the segments and re-mux them as mp4
ffmpeg -i "concat:seg1.ts|seg2.ts|seg3.ts" -c:v copy -c:a copy -movflags empty_moov -flags global_header -bsf:v dump_extra edited-final.mp4

So now we have the whole thing done without ever transcoding the movie, preserving the original quality. But as always, there a caveat...


CAVEAT: The cutting can be done only on a keyframe boundary. Explanation: H.264 organizes the compressed frames in packets, each beginning with a full yet compressed image of the first frame, followed by the deltas of the following frames, thereby decreasing the storage required for each packet. For our purpose, each packet is like a sealed zip of all frames for that duration - it is either all or none. If you want just a piece of the packet then you have to unzip it and zip it back, in other words - to transcode it. So the above method is relevant only if you have a keyframe in each position where you want to cut the movie. For example, if you have a keyframe every 5 seconds you can cut it only every 5 seconds.


So now the question is whether you can accept the limitations on cutting points, particularly as you probably have no idea where in your movie you have keyframes. And that's the reason I suggested above to read in 5.4 Main Options about specifying -s and -t before the input or before the output. If you specify before the input then ffmpeg will find a nearby keyframe to perform your request, which will be "more-or-less" where you wanted to perform the cut. If you don't mind about the preciseness of the cut then good, go for it.


But if you need the cut to be at that precise position, then you have no choice, you must decode the movie in order to pinpoint the frame you're looking for. Well, at least we have some good news: Instead of transcoding and re-transcoding you can do with a single transcode, which somewhat improves the situation:


# skip PRECISELY 30 seconds and transcode a 60 seconds TS segment
ffmpeg -i full-movie.mp4 -ss 30 -t 60 -bsf:v h264_mp4toannexb segment.ts

Since the resulting segments will be in TS, no need for another transcode when merging the segments together.



In the ffmpeg wiki article about merging there's an explanation on how run the whole process through pipes, thereby eliminating the need for intermediate files and speeding the whole process. DON'T. It will take you longer. The reason that doing it all in memory will take longer isn't because it will run longer, but because you will have no intermediate results while figuring how to get the whole thing done, and you'll find yourself running the whole process again and again. So the pipes theory is good, but in your case you should start by working out and perfecting each step. You'll find that getting everything to work and produce a decent result will require some more tweaking and tuning. Once you'll master the entire process and wish to do some scripting for automated mass editing then you can revisit the piping concept.


Hope the above helps.


Comments

Popular Posts

Use Google instead of Bing with Windows 10 search

I want to use Google Chrome and Google search instead of Bing when I search in Windows 10. Google Chrome is launched when I click on web, but it's Bing search. (My default search engine on Google and Edge is http://www.google.com ) I haven't found how to configure that. Someone can help me ? Answer There is no way to change the default in Cortana itself but you can redirect it in Chrome. You said that it opens the results in the Chrome browser but it used Bing search right? There's a Chrome extension now that will redirect Bing to Google, DuckDuckGo, or Yahoo , whichever you prefer. More information on that in the second link.

linux - Using an index to make grep faster?

I find myself grepping the same codebase over and over. While it works great, each command takes about 10 seconds, so I am thinking about ways to make it faster. So can grep use some sort of index? I understand an index probably won't help for complicated regexps, but I use mostly very simple patters. Does an indexer exist for this case? EDIT: I know about ctags and the like, but I would like to do full-text search. Answer what about cscope , does this match your shoes? Allows searching code for: all references to a symbol global definitions functions called by a function functions calling a function text string regular expression pattern a file files including a file

How do I transmit a single hexadecimal value serial data in PuTTY using an Alt code?

I am trying to sent a specific hexadecimal value across a serial COM port using PuTTY. Specifically, I want to send the hex codes 9C, B6, FC, and 8B. I have looked up the Alt codes for these and they are 156, 182, 252, and 139 respectively. However, whenever I input the Alt codes, a preceding hex value of C2 is sent before 9C, B6, and 8B so the values that are sent are C2 9C, C2 B6, and C2 8B. The value for FC is changed to C3 FC. Why are these values being placed before the hex value and why is FC being changed altogether? To me, it seems like there is a problem internally converting the Alt code to hex. Is there a way to directly input hex values without using Alt codes in PuTTY? Answer What you're seeing is just ordinary text character set conversion. As far as PuTTY is concerned, you are typing (and reading) text , not raw binary data, therefore it has to convert the text to bytes in whatever configured character set before sending it over the wire. In other words, when y

networking - Windows 10, can ping other PC but cannot access shared folders! What gives?

I have a computer running Windows 7 that shares a Git repo on drive D. Let's call this PC " win7 ". This repo is the origin of a project that we push to and pull from. The network is a wireless network. One PC on this network is running on Windows 10. Let's call this PC " win10 ". Win10 can ping every other PC on the network including win7 . Win7 can ping win10 . Win7 can access all shared files on win10 . Neither of the PCs have passwords. Problem : Win10 cannot access any shared files on win7 , not from Explorer, nor from Git Bash or any other Git management system (E-Git on Eclipse or Visual Studio). So, win10 cannot pull/push. Every other PC on the network can access win7 shared files and push/pull to/from the shared Git origin. What's wrong with Windows 10? I have tried these: Control Panel\All Control Panel Items\Network and Sharing Center\Advanced sharing settings\ File sharing is on, Discovery is on, Password protected sharing is off Adapte