Copy video except for sections that require re-encoding

Started by Who, March 01, 2021, 07:40:55 PM

Previous topic - Next topic

Who

This is something I have wondered about for a while.  Let's say you have a large video that takes hours to encode, and after it is finished you find one small defect in one section of the video.  The way Avidemux currently works, your only option is to reload the source video, make all your changes and fixes again (or load them from a project script if one is available), fix the bad part, and save the whole thing again.  Somehow I keep thinking there ought to be a more efficient way to do this, without losing quality.

It occurs to me that when you copy a video rather than re-encoding it, your copy must begin and end on keyframes.  So what if there were a checkbox that said something like "copy sections where possible."  The idea in that case is that Avidemux would keep track of what sections were actually edited and then, assuming that the input and output files were in compatible formats, would only copy sections of the video and/or audio that have not been changed, and would only re-encode sections that have been changed (by "sections" I mean parts between keyframes).

Obviously there are a lot of situations where this would fail, particularly where the input and output files are different in some major way.  For example if you applied a crop filter and it made the size of the edited sections different from the non-edited sections, and were saving to a format that does not permit that (and I'm not aware of any formats that would permit that), then it would do as usual and re-encode the entire video, or else maybe pop up a warning explaining that what you are trying to do is not possible.  But on the other hand let's say that as an example you just want to use a black filter (changes the video to black) for a few seconds, which does not change the format of the video in any way.  In that case it would just do a straight copy until it gets to that point and only re-encode the sections (between keyframes) where edits have actually occurred, then if there is a remaining portion of the video it would switch back to copying only.

This could reduce the time required to make small edits (or fix small goofs) from hours to minutes in some cases.  If you only had to re-encode sections between keyframes where something has actually changed and could simply copy all the other sections, that could potentially be a big time saver.  No idea how difficult that would be to implement so I am just making the suggestion.  So far every suggestion I have made has been pooh-poohed (or I have been told there is another way to accomplish it that is much more complicated and that in testing doesn't work) so I suppose this one will be no different, but hope springs eternal.

butterw

If you've encoded a video with x264 and find a defect (ex: 1 source frame has artifacts), you can correct and re-encode only the corresponding section starting and ending from a keyframe.
 
The requirement is that you must use the exact same encoding parameters (easy using presets), you can then simply replace one section with another (possibly using a replace insertion script).



Who

butterw I have no idea what you mean.  If you are trying to say there is already a way to do this in Avidemux then please explain how.  If you are trying to say there is a way to do this that is complicated and involves using software other than Avidemux, then that's not what I was asking about and kind of defeats the whole point.  But either way, you are not giving enough of an explanation that I have the slightest idea what you mean.

butterw

If you have encoded a video, it is currently possible to re-encode only a single section and then substitute it in your original encode (it must just start and end on a keyframe, and have matching encoding parameters).

An option could be added to my bInsert.py script to avoid having to do the append, cut and paste operations manually. I may attempt to write a script for the black frame filter.

Scripting capacity has increased significantly in v2.7.7. As of r210215_ handling video segments is possible. IIRC some of your previous suggestions are currently possible quite simply via scripts which can be added to the GUI. To go further changes to the Avidemux GUI would be required.
 

szlldm

You save with copy mode the preceding half, then the part after the bad segment (both cut at keyframe). Fix and reencode the bad part, then append the 3 part together.

eumagga0x2a

I have to warn about possible short gaps in audio at the start and at the end of insertion if B-frames are used.

Who

Quote from: butterw on March 02, 2021, 09:50:40 AMIf you have encoded a video, it is currently possible to re-encode only a single section and then substitute it in your original encode (it must just start and end on a keyframe, and have matching encoding parameters).

An option could be added to my bInsert.py script to avoid having to do the append, cut and paste operations manually. I may attempt to write a script for the black frame filter.

Scripting capacity has increased significantly in v2.7.7. As of r210215_ handling video segments is possible. IIRC some of your previous suggestions are currently possible quite simply via scripts which can be added to the GUI. To go further changes to the Avidemux GUI would be required.
 
I only mentioned the black frame filter as an example of something that would not change the size or format of the video in any way.  In actual use I would probably want to use some other filter or combination of filters that do not change the video format.

But in any case, I was not really wanting to do this using scripts.  None of the existing features or filters involve using scripts.  If I have to use scripts to manually join sections of video then I might just as well use ffmpeg.  My intention would be that this would be something any user could do, not just those that have a familiarity with scripts.  But the bigger problem is what eumagga0x2a mentions, that when you start trying to do things that are not natively built into Avidemux, in my experience it almost always messes up the audio in some way.  For example, a few weeks ago I had asked about something else where I wanted to edit the video (replacing a section of video with another video of the same exact length) without changing the audio, and was told that I could save the original audio to a file, make the edits, and then select the saved audio file as the audio track.  But when I tried that, the audio was slightly but noticeably out of sync throughout the entire video, including the portion before the edit point where nothing had been changed.  It makes no sense at all to me why that should happen, but it does.

So when you say, "To go further changes to the Avidemux GUI would be required", that's exactly what I am suggesting.  I can accept that some things may not be possible in Avidemux for reasons I probably would not understand, but I'm specifically not looking for solutions that involve having to go outside the Avidemux GUI to accomplish something, both because I have a very difficult time understanding them (I am not a programmer) but also because they invariably seem to have unintended and unwanted side effects.

eumagga0x2a

Quote from: Who on March 02, 2021, 02:42:56 PMa few weeks ago I had asked about something else where I wanted to edit the video (replacing a section of video with another video of the same exact length) without changing the audio, and was told that I could save the original audio to a file, make the edits, and then select the saved audio file as the audio track.  But when I tried that, the audio was slightly but noticeably out of sync throughout the entire video, including the portion before the edit point where nothing had been changed.

There are different things at play:

1. An old bug, fixed a while ago, resulted in the first 2-3 frames of raw AC3 streams lost when imported as external audio. This could advance audio relative to video by ~ 100 ms.

2. I may be wrong here, but I suspect that external audio tracks are not delayed to account for B-frames in video, in this case an external audio track needs to be manually delayed by the number of milliseconds displayed as the PTS of the first (key)frame.

3. If spliced and joined again video contains B-frames, there will be a gap in PTS equal the B-frame delay. If audio was exported prior to splicing and re-imported after merging, I would expect it to be farther ahead of video by the B-frame delay after each merge point.

Choosing encoding modes or codecs without B-frames (e.g. VP9) for video and using PCM as export and import format for audio tracks avoids a lot of hassle.

butterw

To avoid sync issues as much as possible, saving all modifications as a project and only encoding as a final step is the safest approach.

Who

I give up.  You guys insist on talking way over my head.  I am looking for something that "just works" - if that's not possible then it's not possible, thank you very much and I'll just go away now.  But you are wasting your time typing all these convoluted explanations to me because I don't understand a bit of it.

szlldm

Maybe you overlooked it:
QuoteYou save with copy mode the preceding half, then the part after the bad segment (both cut at keyframe). Fix and reencode the bad part, then append the 3 part together.

If there is a problem with the audio after appending, you still can merge in the audio fom the original file.