can i add a small external video in to the middle of existing video by avidemux

Started by jraju, January 14, 2021, 10:46:49 AM

Previous topic - Next topic

butterw

Quote from: eumagga0x2a on January 16, 2021, 12:24:10 AM
Quote from: eumagga0x2a on January 15, 2021, 01:13:57 AM
Quote from: butterwThen after you cut, you also need to delete the first frame of the cut section, (not currently deleted by default when you cut to the end).

The idea is to avoid visual disruption for the user as she or he will have probably exactly this first frame of the appended video loaded in the video preview when cutting.

I've modified the behaviour of truncateVideo() to match removeChunk() with regard to the start of selection i.e. to delete the frame at the A marker. If the start of selection matches the first frame of an appended video, the whole segment is deleted.

This brings back the UX problem of loss of current visual context, slightly mitigated as Avidemux now tries to jump to the previous keyframe instead of rewinding to the start of video.

Losing visual context might be a thing, but at least the user knows what has just been deleted.
Moving the view to the frame just before the segment to be deleted (now the last frame of the video) seems like the most logical step (this is also the frame you need to be on currently to delete to the End).

Who

Quote from: eumagga0x2a on January 16, 2021, 12:30:36 AM
Quote from: Who on January 15, 2021, 11:40:50 PMAnd if I knew exactly how much it was off I could correct it using the audio Shift field, but I don't, and I detest the idea of trial-by-error

Are you aware of audio shift working in preview? It is not that you need to save video each time you modify the value. Of course, one needs to put some effort into finding the right sync, but this takes usually just a couple of minutes.

I have read that over several times, and either I am still not understanding it or you totally ignored what I said about not wanting to use a trial-and-error method.  First, you ask "Are you aware of audio shift working in preview?"  But I am not talking about the audio being off in preview, I am talking about it being off in the saved file.  You then say "It is not that you need to save video each time you modify the value."  The only thing I can deduce from that is you are saying to try different values and watch in preview to see when it looks right, if so that is totally abhorrent to me, I would NEVER EVER IN A MILLION YEARS try to do sync using just my eyes and ears, because that is not at all precise!  Then you say "Of course, one needs to put some effort into finding the right sync, but this takes usually just a couple of minutes" which again leads me to suspect you totally ignored what I said and are suggesting I just try various values until I hit one that "looks" right.

Here is the thing, if I start with a video that has audio that is in sync with the video, and somewhere during the editing process it loses that synchronization, then the resulting file and maybe the original file is toxic waste as far as I am concerned!  NORMALLY Avidemux is really good about keeping the audio in sync, even after multiple edits. It is only if I try to save the audio to a separate file and then bring it back where it doesn't precisely sync up with the video, and that is true even near the start if you only made edits near the end.  And no I am not going to try various random shift settings to see what "looks good", if that is what you are actually suggesting, that is NOT the correct way to sync up audio with video!  To me that's like if I needed to get some paint to repair a damaged section of wall and I went to the paint store and tried to pick a color based on my memory of what color the wall is.  Even if somehow I managed to get close, it would never be a precise match, and it would always really bother me that it isn't precise.

Also, I have seen occasional situations where the audio looks VERY out of sync in the preview mode (off by even a second or two) but still is perfectly synced in the saved file (obviously these are cases where I did not try to save the audio to a separate file and then bring it back).  My suspicion in those cases are that the files are some high-bandwidth variety and it is my hardware that can't quite keep up but in any case that's just another reason I would never attempt to sync audio and video based on what I see in a preview.  If I am in doubt, I always do a test encode of a few seconds of the video and watch that in an external player such as VLC.

Quote from: eumagga0x2a on January 16, 2021, 12:30:36 AMBy the way, I think some issues with external audio causing loss of sync were fixed recently, e.g. for AC3.

That may be the issue then; several of the files I have attempted to edit have been encoded with AC3 audio.  So if it was fixed recently that means I have to wait for the next release version that will work in MacOS (Catalina).  Thanks for fixing that!

I guess what I am wondering is whether it would really be this hard to add the ability to replace a number of frames of video with frames from a different video.  You can replace single frames or a series of frames with a static image that replaces the entire frame(s) or a portion thereof, and this would simply be replacing a series of frames with a different series of frames taken from another video instead of a single static image.  I am not a programmer so I have no idea what would be involved, but when I see some of the amazing filters that already exist in Avidemux, this seems to me like something that really shouldn't be that difficult, and the main thing is that it would not involve saving and reloading the audio so that there would be no chance at all of introducing audio/video sync issues.  It would also be a lot easier method of replacing a section of video than what you suggested in that other thread, but maybe that's kind of beside the point if programming such a feature would take up far too much time.  What I REALLY care about is preserving the ORIGINAL audio/video sync in the part of the video that has no been changed, not trying to guess what it should be afterwards.  Anyway, I'm not going to beat a dead horse here if you have really decided this can't be done or if it would take far too much time and effort, I'll just say that if you ever do think of a way to add something like that it would really be appreciated, and I suspect not just by me.

eumagga0x2a

Quote from: butterw on January 16, 2021, 01:03:58 AMMoving the view to the frame just before the segment to be deleted (now the last frame of the video) seems like the most logical step

Sure, but not viable as the seek to a frame which is likely located at the end of a GOP may take a lot of time depending on codec, GOP length, video resolution and hardware capabilities.

eumagga0x2a

Quote from: Who on January 16, 2021, 12:29:36 PMBut I am not talking about the audio being off in preview

I didn't mean that.

Quote from: Who on January 16, 2021, 12:29:36 PMYou then say "It is not that you need to save video each time you modify the value."  The only thing I can deduce from that is you are saying to try different values and watch in preview to see when it looks right,

Exactly, the step I never leave out. On my system, achieving a good sync in Avidemux during preview results in a good sync after saving when played in video players or on external devices.

Quote from: Who on January 16, 2021, 12:29:36 PMI would NEVER EVER IN A MILLION YEARS try to do sync using just my eyes and ears, because that is not at all precise!

It is actually quite the opposite, eyes and ears are ultimately the only valid reference. But I understand that once audio is more that just a half a second off, it becomes almost impossible to sync from lips or from hand movements (e.g. of a musician) without clear visual references like a clap.

Quote from: Who on January 16, 2021, 12:29:36 PMIt is only if I try to save the audio to a separate file and then bring it back where it doesn't precisely sync up with the video

This is correct and a big fundmental problem. In a raw audio stream, there is no other source of timing than counting audio samples coming out of the decoder for given sampling rate. In a container with video, a player can compare audio timing with video timing and perform a sync if they don't match. Different applications have different sync strategies. Most players will try to sync video to audio, Avidemux will sync (by inserting silence or skipping samples) audio to video. But when saving audio as a raw audio stream, Avidemux cannot do that. If audio timing in the source container didn't 100% agree with the number of samples decoded, exporting and importing audio will result in A/V getting out of sync somewhere.

Quote from: Who on January 16, 2021, 12:29:36 PMAlso, I have seen occasional situations where the audio looks VERY out of sync in the preview mode (off by even a second or two) but still is perfectly synced in the saved file [...].  My suspicion in those cases are that the files are some high-bandwidth variety and it is my hardware that can't quite keep up

Indeed, high frame rates at high resolutions with computationally expensive codecs (AV1, HEVC, to a lesser extent VP9), probably decoded in software rather than in hardware will do that (and even with hardware acceleration available, at least on macOS Avidemux does a poor job using it as it always immediately downloads decoded video data from GPU to the normal memory and then re-uploads it for display). Such files are not really editable as A/V sync cannot be judged during preview.

Quote from: Who on January 16, 2021, 12:29:36 PMSo if it was fixed recently that means I have to wait for the next release version that will work in MacOS (Catalina).

Unless you need AV1 decoding and VP9 encoding support, you can use the latest (not very fresh but oh well) Mojave nightly from https://avidemux.org/nightly/osx_mojave/ as it has that code change. In general, the idea is to build all macOS builds on Mojave as there is no easy way to generate backward-compatible builds on Catalina or Big Sur.

Quote from: Who on January 16, 2021, 12:29:36 PMAnyway, I'm not going to beat a dead horse here if you have really decided this can't be done or if it would take far too much time and effort

There are simply no resources to investigate this at the moment, apart from the fact that AFAIK you can do it in iMovie.

butterw

From what I understand Avidemux uses Tinypy for its project files and custom user scripts (available in the menu).
Would it be possible to write an insert.py script with Tinypy (when the next dev build becomes available) ? 

Easy Lossless insert:
- Position yourself on a keyframe
- Select Insert command >> choose the clip to be inserted in a file open dialog (or paste the filepath)
- Avidemux inserts the clip before the keyframe (warns of possible issues if applicable).

butterw

From what I understand Avidemux uses Tinypy for its project files and custom user scripts (available through the menu).
https://www.avidemux.org/admWiki/doku.php?id=using:tinypy

Is it currently (when the next dev build becomes available) possible to write an insert.py script with Tinypy ? 

Easy Lossless insert:
- Position yourself on a keyframe
- Select Insert command >> choose the clip to be inserted in a file open dialog (or paste the filepath)
- Avidemux inserts the clip before the keyframe (warns of possible issues if applicable).

Who

Quote from: eumagga0x2a on January 16, 2021, 02:19:40 PMThis is correct and a big fundmental problem. In a raw audio stream, there is no other source of timing than counting audio samples coming out of the decoder for given sampling rate. In a container with video, a player can compare audio timing with video timing and perform a sync if they don't match. Different applications have different sync strategies. Most players will try to sync video to audio, Avidemux will sync (by inserting silence or skipping samples) audio to video. But when saving audio as a raw audio stream, Avidemux cannot do that. If audio timing in the source container didn't 100% agree with the number of samples decoded, exporting and importing audio will result in A/V getting out of sync somewhere.

Well maybe this is a dumb question, but if "Avidemux will sync (by inserting silence or skipping samples) audio to video" I would assume that at that point it has a count of how much silence it has to add, or how many samples it has to skip.  What if it saved that offset value in a variable, and then if you exported the audio it would also save that offset value in a small file with the same filename as the audio file but a different extension?  And then when you go to bring the audio back it would look at that offset file and use that offset value?  Or, alternately, maybe when you go to save audio it could just print something to the screen saying "the current audio offset is xxx ms" and then when you go to bring it back you could use that value in the audio offset box?  Just anything so you get an exact sync and are not trying to guess the sync amount.

Even with a clapboard in the video I would not feel comfortable trying to judge the offset using my eyes and ears.  It would maybe be different if Avidemux had an expanded audio waveform display (no I am not asking for that) so that you could see visually exactly where the sound occurred in relation to the clapboard or whatever, and then drag or reposition the audio so it lines up; then I would feel much more confident that I could get an accurate match (but only if I had a good visual and audio cue to go on, which I'm sure would not always occur).  But still I think it is far better to try to preserve the original audio offset from the source material, and I would not mind entering that value by hand, I just don't want to be trying to "guesstimate" what it should be.

Quote from: eumagga0x2a on January 16, 2021, 02:19:40 PMUnless you need AV1 decoding and VP9 encoding support, you can use the latest (not very fresh but oh well) Mojave nightly from https://avidemux.org/nightly/osx_mojave/ as it has that code change. In general, the idea is to build all macOS builds on Mojave as there is no easy way to generate backward-compatible builds on Catalina or Big Sur.

That should work fine in Catalina, so I downloaded and installed it and will see if it makes any difference.  Thanks!

butterw

Quote from: butterw on January 17, 2021, 10:54:58 AMFrom what I understand Avidemux uses Tinypy for its project files and custom user scripts (available through the menu).
https://www.avidemux.org/admWiki/doku.php?id=using:tinypy

Is it currently (when the next dev build becomes available) possible to write an insert.py script with Tinypy ? 

Easy Lossless insert:
- Position yourself on a keyframe
- Select Insert command >> choose the clip to be inserted in a file open dialog (or paste the filepath)
- Avidemux inserts the clip before the keyframe (warns of possible issues if applicable).

It should be possible with Tinypy, but adm.clearSegments and adm.addSegment still remain a bit unclear to me.
   
Is there a way to add a complete Segment without knowing its duration (rather than adm.addSegment(1, 0, 10000000))
OR a way of getting the appended segment duration directly
OR a way of resetting markers like in the GUI without calling adm.clearSegments() ?

adm.loadVideo("B:/Videos/out/vid.mp4")
markA = adm.markerA
end0 =  adm.markerB
adm.appendVideo("B:/Videos/out/ins.mp4")
adm.clearSegments()
adm.addSegment(0, 0, markA) #presumably I need to stop one frame before keyframe markA
adm.addSegment(1, 0, 10000000) #I need duration of Segment 1. Is it possible to get directly or does it need to be calculated ?
adm.addSegment(0, markA, end0)
EDIT: end = ed.getVideoDuration() is equivalent to adm.markerB and involves a bit of calculation to reset the final value of marker B (which seems to be required.)

I got a popup msgbox on saving:
Cut points could not be checked.  This indicates an issue with a source video, the state of editing or a bug in the program.  Please check the application log file or console output for details.  Try anyway?

eumagga0x2a

If the first video wasn't edited yet (a single segment from 0 to its full duration fullDuration_0) , then the first segment is

adm.addSegment(0,0,currentPts)

The second one is the appended video:

adm.addSegment(1,firstFramePts_1,fullDuration_1)

The problem here is to get firstFramePts_1, i.e. the offset of the first frame relative to the start of the time in that reference video.

The third one is

adm.addSegment(0,currentPts,fullDuration_0)

eumagga0x2a

Editing commands are not available via internal Python interface, which means that the entire segment handling logic has to be reimplemented in Python.

butterw

Quote from: eumagga0x2a on January 17, 2021, 10:22:27 PMEditing commands are not available via internal Python interface, which means that the entire segment handling logic has to be reimplemented in Python.

You mean the commands from the Edit Menu (except for Set Markers) are not directly available in Tinypy,
meaning: Cut, Copy, Paste, Delete, Undo, Redo
Reset Markers, Reset Edit have some similarity with adm.clearSegments() + adm.addSegment() for all segments.

butterw

Quote from: eumagga0x2a on January 17, 2021, 10:20:19 PMIf the first video wasn't edited yet (a single segment from 0 to its full duration fullDuration_0) , then the first segment is
adm.addSegment(0,0,currentPts)

The second one is the appended video:
adm.addSegment(1,firstFramePts_1,fullDuration_1)

The problem here is to get firstFramePts_1, i.e. the offset of the first frame relative to the start of the time in that reference video.

The third one is
adm.addSegment(0,currentPts,fullDuration_0)

- ed.currentPts() returns the timestamp of the current position.
- from what I understand fullDuration_1 needs to be calculated, there is no predefined function for this.
- How is firstFramePts_1 obtained in regular Avidemux ?

As long as the source video has not been edited and only has one segment, operations such as split and insert look like they could be achieved by a script.


eumagga0x2a

Quote from: butterw on January 18, 2021, 01:29:45 AM- How is firstFramePts_1 obtained in regular Avidemux ?

Please see ADM_EditorSegment::updateRefVideo() in avidemux/common/ADM_editor/src/ADM_segment.cpp

eumagga0x2a

From Python interface, you would need to get the PTS of the first keyframe of the appended video (iterate ed.getNextKFramePts() until the value != ADM_NO_PTS is > fullDuration_0), then subtract fullDuration_0.

butterw

Quote from: eumagga0x2a on January 18, 2021, 08:37:53 AMFrom Python interface, you would need to get the PTS of the first keyframe of the appended video (iterate ed.getNextKFramePts() until the value != ADM_NO_PTS is > fullDuration_0), then subtract fullDuration_0.

What does ADM_NO_PTS mean and is its Python value (-1) ?
If I understand correctly I would need to seek with adm.seekKeyFrame, until ed.getNextKFramePts()>fullDuration_0 ?
fullDuration_0 can be obtained simply by ed.getVideoDuration() before appending.

The Python binding does not have all the Go menu functions, only next frame, and seek N keyframes.
adm.seekKeyFrame(int) #updates the preview
ed.nextFrame() #returns 0 on the last Frame
ed.getCurrentPts()
ed.getPrevKFramePts()
ed.getNextKFramePts() #returns -1 after the last KFrame