2.7.6 Smart copy mode & Keyframe seek methods in Python interface

Started by Alex_Andre, January 20, 2021, 05:52:51 AM

Previous topic - Next topic

Alex_Andre

Hello everyone,

It's my first message in this forum. So to start with, thank's a lot to the development team for this nice piece of software and to the community for the support.

I started to dig up into scripts recently.
I'm facing a problem with keyframes, and i couldn't find a solution online so i'm giving a try here.
Here is the simplified Process:
Input file: mp4, 720p, 30FPS
Output: "Copy"
1. LoadVideo
2. Set MarkerA (Beginning) & MarkerB (1~2sec before the end)
3. Save
4. Set MarkerA (=MarkerB 1~2sec before the end) & MarkerB (End)
5. Save

Problem :

It works fine until 3, but with 4 & 5, I get some problems related to keyframes.
If I place it the marker at random place (VideoLength – x sec), the keyframe will be missing and it seems that the Script is not able to use the "smart copy mode" (as it does in the Avidemux interface, i never have problem of saving any segment)
I also tried to find a walk around to locate the MarkerA at a round number of second to luckily find a key frame. It works for some short videos, but I'll always struggle if i use the same code for another video...
It looks like the keyframes are located every 1.000sec = every 30FPS (Apparently, by navigating with the UP and DOWN keys in Avidemux interface).

Ideas :

1) To actually solve the problem, is there a way to save the section using the "smart copy mode", let Avidemux re-render the first broken GOP?

2) I saw in the latest release note (2.7.6) that a new function has actually been added:
"* Scripting:Add PTS query and keyframe seek methods to Python interface"
http://avidemux.sourceforge.net/news.html#2020-07-07

This would be a nice walk around if 1) doesn't work. But I can't find any information about this function (Name? arguments? etc)
I tried to check in the source code of Avidemux 2.7.4 but we're getting way out of my comfortable zone here...

eumagga0x2a

Quote from: Alex_Andre on January 20, 2021, 05:52:51 AMIf I place it the marker at random place (VideoLength – x sec), the keyframe will be missing and it seems that the Script is not able to use the "smart copy mode" (as it does in the Avidemux interface, i never have problem of saving any segment)

There is no "smart copy" mode in Avidemux. However, if in copy mode the start of selection (the A marker) is not set to a keyframe, Avidemux will silently rewind to the previous keyframe before starting output. This is performed by code which is invoked equally by GUI and by scripting, so that it should not make any difference (I've just tried it using a build generated off the latest git master and it indeed works fine for me, the saved video starts at a keyframe which is located ~4 seconds before the A marker which I had put 2 seconds before the end of video).

Quote from: Alex_Andre on January 20, 2021, 05:52:51 AMIt looks like the keyframes are located every 1.000sec = every 30FPS (Apparently, by navigating with the UP and DOWN keys in Avidemux interface).

Keyframes are located where the encoder, which created the video stream, placed them.

Quote from: Alex_Andre on January 20, 2021, 05:52:51 AM1) To actually solve the problem, is there a way to save the section using the "smart copy mode", let Avidemux re-render the first broken GOP?

No, this is not feasible. It may be even fundamentally impossible with modern open-GOP types of H.264 and HEVC streams used by practically all broadcasters.

So far, if the question is only about producing playable output in copy mode despite the start of selection being not on a keyframe, this should just work, no tricks needed. Please try the latest nightly (2.7.6 is really old now).

If it doesn't work for you, we'll need to look at the file.

The seekKeyFrame(int n) method in Avidemux() may be helpful to save each n-th keyframe as images when called in a loop with e.g. saveJpeg(filepath), should not be necessary for your task.

butterw

In the latest dev version I have observed the following issue relative to this new feature:
- In Copy Mode, I set the position randomly by clicking with the mouse.
- I then set MarkerA (Selection starts on the keyframe before the current position)
- When I try to seek to the previous keyframe, it goes to the keyframe before, not to the start of the selection as expected.

eumagga0x2a

Quote from: butterw on January 20, 2021, 11:54:45 PM- In Copy Mode, I set the position randomly by clicking with the mouse.
- I then set MarkerA (Selection starts on the keyframe before the current position)

No, it starts at the keyframe equal the current position. Don't mistake position of the mouse event with the position of the keyframe, see handling of ACT_Scale in HandleAction_Navigate.

Quote from: butterw on January 20, 2021, 11:54:45 PM- When I try to seek to the previous keyframe, it goes to the keyframe before

As asked for.

butterw

I think I now understand how avidemux2.7 operates.

The issue observed is that clicking the mouse, sets the cursor position in the GUI but it doesn't reflect the actual position (which is set to the previous keyframe).

This is also true outside of Copy Mode.

Why not make the cursor position set by mouse-click stick to the actual position ?

It would be much less confusing.
This only became clear to me because I was cutting a very short video (ex: 15s) with only a few keyframes and clicking seemed like the most natural way to do it.