Is there any way to replace a section of video without changing the audio track?

Started by Philo, February 17, 2022, 10:38:01 PM

Previous topic - Next topic

Philo

I have looked through all the filters in Avidemux but if there is one that will do this I'm not finding it.

What I would like is a filter that would allow replacing a segment of video with a completely different video from another file, assuming both files have the same formats and dimensions.  Ideally I'd like to specify the starting point and then it would replace the video on a 1:1 basis, in other words one frame from the original video would be replaced by one frame from the replacement video until the replacement video ends.  But I don't want to touch the underlying audio track in any way.  I do realize that the video would probably need be be re-encoded, but I would probably just leave the audio set to "Copy."

Does this exist and I'm just not seeing it?

eumagga0x2a

You want to append the second video and use the basic editor functionality (copy, cut = copy + delete, paste) to achieve your goal. No filtering involved.

Philo

Quote from: eumagga0x2a on February 17, 2022, 11:15:45 PMYou want to append the second video and use the basic editor functionality (copy, cut = copy + delete, paste) to achieve your goal. No filtering involved.

Apparently you didn't even bother to read what I wrote before you dashed off your quick response.  Your "solution" is not even close to what I am wanting to do.

eumagga0x2a

If the answer you got doesn't fully fit your expectations, it doesn't mean it is incorrect. No filter in Avidemux has any idea of what happens outside of its "box", especially not about the ultimate source of pictures which the previous filter in the chain passes to it, so based on the design of the application, your request is a complete non-starter. On the other hand, exporting and then reimporting the audio track to sort of preserve the audio from the first video while the second one is shown, is relatively easy and unavoidable in Avidemux.

Philo

Quote from: eumagga0x2a on February 18, 2022, 08:20:36 PMOn the other hand, exporting and then reimporting the audio track to sort of preserve the audio from the first video while the second one is shown, is relatively easy and unavoidable in Avidemux.

And herein lies the problem, I have no interest whatsoever in "sort of" preserving the audio. I do not want the audio to be touched, at all.  The reason I am having a hard time with your answer is because in Avidemux you can do all kinds of things to the video without affecting the audio.  You can replace frames with black, or overlay just a portion of the video with black.  You can stretch or shrink the image.  You can replace a section of video with a single still image.  What I was hoping could be done seemed obvious - instead of doing some static modification to a series of frames, just replace one from from the original video with one frame from a replacement video, then move to the next frame and do the same thing.  You act like I want to build the pyramids using only feathers as tools!

The reason I don't like the export and reimport trick (and will not ever do that) is because there is no way to guarantee that the re-imported audio will precisely sync up with the video.  I suspect you probably knew that and that is why you used the words "to sort of preserve the audio".  "Sort of" just isn't good enough for me, I don't want that audio to be shifted even one millisecond from where it originally was.  I don't want it to be shifted one waveform from where it originally was!  Can you absolutely guarantee that if I export the audio and then reimport it that it will be in exactly the same place as it originally was (assuming, of course, that I do not change the number of video frames in the video)?

I'm not going to play the game of, "if I stare at it long enough I can convince myself that the audio and video are in sync."  And I'm definitely not going to try to make them line up by offsetting one from the other by a few milliseconds until the sort of look right and hoping they are close.

So I guess for what I want to do, Avidemux is a complete non-starter.  Sorry to have wasted your time asking for something that sure seemed at first glance like a logical extension of your existing filters, in fact I originally assumed that it was already possible and I just wasn't finding the correct filter.

eumagga0x2a

Quote from: Philo on February 19, 2022, 09:41:23 PMI have no interest whatsoever in "sort of" preserving the audio. I do not want the audio to be touched, at all.

Saving the audio track of an H.264 + AAC MP4 video loaded in Avidemux to a file and re-importing it, then remuxing the video in copy mode to another MP4 file, loading that file in Avidemux, inspecting A/V sync and saving the audio track again showed that sync was perfectly (well, deviations < 10 ms are too small to notice even with my test video) preserved and both audio track exports were bit-identical. So at least for audio codecs with constant number of samples per packet, I would not be too much concerned about losing sync purely from re-importing the audio track.

Quote from: Philo on February 19, 2022, 09:41:23 PMCan you absolutely guarantee that if I export the audio and then reimport it that it will be in exactly the same place as it originally was (assuming, of course, that I do not change the number of video frames in the video)?

Obviously not. This starts with different ways to represent timing in a container, e.g. Matroska has 1 millisecond resolution which means that timing in videos with not strictly 25 or 50 fps is always an approximation.

The real problem lies elsewhere. "Overlaying" video A starting with time offset T with video B while keeping audio from A is really trivial when 1) T + duration of B < duration of A and 2) we never switch back to A after time T. But "continuing" with A after B is finished and preserving A/V sync may be a challenge because imitating overlay / video track switch with copy / paste / delete operations means that we need to delete a range of A starting with T and equal the duration of B.

Quote from: Philo on February 19, 2022, 09:41:23 PMYou can replace frames with black, or overlay just a portion of the video with black.  You can stretch or shrink the image.  You can replace a section of video with a single still image.

None of these operations require access to the infrastructure of demuxers and video decoders which only the editor has. What actually could be written, is a filter which would load sequentially named image files (*.jpg or *.png), like the "Add logo" filter, but with logo automatically changed each frame. But the use case indeed seems to be better covered by existing full-blown video editors which can operate on multiple video tracks.

Quote from: Philo on February 19, 2022, 09:41:23 PMSo I guess for what I want to do, Avidemux is a complete non-starter.

If conditions 1 and 2 listed above are not met, I agree here.

Philo

Quote from: eumagga0x2a on February 20, 2022, 01:08:18 AMWhat actually could be written, is a filter which would load sequentially named image files (*.jpg or *.png), like the "Add logo" filter, but with logo automatically changed each frame.

Actually, I think perhaps that would work, since I believe ffmpeg has a way to export a series of frames as still images.  I'd need to check into that but I think that would do the trick.

Quote from: eumagga0x2a on February 20, 2022, 01:08:18 AMBut the use case indeed seems to be better covered by existing full-blown video editors which can operate on multiple video tracks.

That may be true, but one of the reasons I have been using Avidemux is because of its relative ease of use compared to some of the more complicated editors.  For some reason I just can't wrap my brain around how they work.  I guess it's like trying to use any fairly complicated program; if you have someone to show you how to get around it becomes a whole lot easier, but if you are just starting out blind absolutely nothing makes sense.  The big advantage Avidemux has is that doing simple operations, like editing out parts you don't want, are fairly intuitive, and then as you play with the filters you discover the other things it can do.  But every time I have tried one of those more complicated editors, I just can't even figure out where to begin.  I think some of those programs can take even the easiest task and make it complicated.  So that is why I was hoping this task could also be accomplished in Avidemux.

Also, as an aside, you mentioned the "Add Logo" filter.  I know it gives to a way to specify a fade in/out time but there have been a few occasions where I have wished you could specify the fade-in and fade-out times independently. I do realize that if the section is long enough you can use the filter twice, once with the correct fade in and then again with the correct fade out and have them overlap in the middle, so that one fades in before the other fades out.  But on short replacements that doesn't always work as intended, so if those two time values could be separated it would be helpful in certain circumstances.  Perhaps have a checkbox to link the two times together, which would be checked by default, and if you want to specify them independently then you uncheck the box and change the values as you wish?

eumagga0x2a

Quote from: Philo on February 20, 2022, 06:48:01 PMsince I believe ffmpeg has a way to export a series of frames as still images

In Avidemux: "File" --> "Save as Image" --> "Save Selection as JPEG".

Saving selection as a row of PNG images will be available if this pull request by szlldm gets merged. Until then, you can use the following tinyPy script:

adm = Avidemux()
ed = Editor()
ui = Gui()

if not ed.nbSegments():
    ui.displayError("No video", "Please load a video prior to running this script")
    return 0

start = adm.markerA
end = adm.markerB
if start > end:
    swap = end
    end = start
    start = swap

if not adm.setCurrentPts(start):
    ui.displayError("Seek error", "Cannot seek to the start of the selection")
    return 0

outfile = ui.fileWriteSelectEx("Select output filename", "png")
if outfile is None:
    return 0

dia = DialogFactory("Set Limits")
imgMax = DFInteger("Maximum # of images:", 1, 99999)
dia.addControl(imgMax)
if not dia.show():
    return 0

count = 0
fileNameNoExtension = (splitext(outfile))[0]

for frame in range(99999):
    if frame >= imgMax.value:
        break
    if frame < 10:
        filler = "0000"
    elif frame < 100:
        filler = "000"
    elif frame < 1000:
        filler = "00"
    elif frame < 10000:
        filler = "0"
    else:
        filler = ""
    if not adm.savePng(fileNameNoExtension + filler + str(frame) + ".png"):
        break
    count += 1
    if not adm.seekFrame(1):
        break
    pts = ed.getCurrentPts()
    if pts >= end:
        break

if not count:
    ui.displayError("Error", "No PNG images saved")
    return 0

ui.displayInfo("Finished", str(count) + " PNG images saved")
return count

Quote from: Philo on February 20, 2022, 06:48:01 PMAlso, as an aside, you mentioned the "Add Logo" filter.  I know it gives to a way to specify a fade in/out time but there have been a few occasions where I have wished you could specify the fade-in and fade-out times independently. I do realize that if the section is long enough you can use the filter twice, once with the correct fade in and then again with the correct fade out and have them overlap in the middle, so that one fades in before the other fades out.

Unfortunately, using two overlapping partialized instances won't work when the logo is not fully opaque. I understand the problem, even if not many users might need such feature, this is a valid feature request.