News:

--

Main Menu

How to automate same cut & save operations?

Started by Silox, February 13, 2024, 01:10:03 AM

Previous topic - Next topic

Silox

Hi guys,

so I have a question about using Avidemux and trying to automate some steps. So my goal is it to find and execute some parameter / command-line what does the job I do normally manually inside of Avidemux.

Task: Pretty simple. I just wanna cut away 10 seconds of video & audio stream at the beginning and then I wanna save it as new file. All just in copy mode and keyframe cut (always there at second 10). Is that possible to do that with Avidemux and if yes how would the command-line look? So I never tried using the CLI version Avidemux before and I don't know the commands yet. I found some info's about it but no info or example when trying to cut something away.

I also tried to do those steps with ffmpeg CLI tool but unfortunately the final results are not so good and I got playback issues in the new file. Some kind of pause at the start & or some frame dropping at the start too which is bad of course. Otherwise when doing the same with Avidemux GUI manually then the final results in the new video file are 1A and I don't get any playback issues in any player. No idea why but that's the different when using ffmpeg & Avidemux. My simple ffmpeg command is this...
ffmpeg -i "Input.mp4" -ss 00:00:10 -c copy "Output.mp4"...and it works to cut at the nearest keyframe at this timestamp but as I said I got those playback issues only in media player not in video editors to see. So the best results I get with Avidemux and now I would like to try it with CLI command too to see whether the output results are same good as when doing the steps inside of Avidemux GUI. So maybe you can tell & help me with that to check this out if it's possible. Thanks.

eumagga0x2a

The Avidemux command line would be in general as follows:

avidemux3_cli --load "/Path/to/Input.mp4" --run "/Path/to/script/to/set/markerA" --video-codec copy --audio-codec copy --output-format MP4 --save "/Path/to/Output.mp4" --quit
On Windows, the name of the CLI executable is different (IIRC avidemux_cli.exe) and paths use backslashes as directory separators, but apart from that there is no difference. A simple script to set the marker A to 10 seconds would be

adm = Avidemux()
ed = Editor()
if not ed.nbSegments():
    return
if ed.getVideoDuration() < 10*1000*1000:
    return
adm.markerA = 10*1000*1000
adm.markerB = ed.getVideoDuration()

If you want non-default options for the MP4 muxer, you need to set the muxer in the script, not on the command line, e.g.

adm.setContainer("MP4", "muxerType=0", "optimize=0", "forceAspectRatio=False", "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")
to disable rewriting the output moving the index to the head of the file.

Silox

Hello eumagga0x2a,

thank you for the help. I tried your script and it seems to work good.  :) Only issue I have is that it takes much longer time to finish the process compared to using ffmpeg CLI directly. Is there a way to increase the process? Otherwise what does AviDemux else than using my ffmpeg command directly? Does AD use other parameters to handle those steps? As I said, when using ffmpeg directly trying to make any keyframe cut nearest to close to next keyframe at timestamp XY then I get those problem I told you about in my first post. Just wanna know how to make it work same good as when using AD.

Off Topic: Why is it not possible to keep all filter settings active when loading a new file? Just can create a custom script to make it work & load it each time. But I see it's also not working to write a script with deactivated filters. Lets say I have 10 filters then I just can load them enabled and need to disable all others manually etc. Just only a question you know. Thanks.

eumagga0x2a

Is Avidemux much slower than ffmpeg even if you disable optimization of the written file for streaming as mentioned?

Filter chain is often tied to specific properties of the source video and segment layout, therefore it is not preserved after the video is closed. Custom scripts are the way to go for all those who know what they are doing.

Handling of disabled scripts is not thought out, I agree.

Silox

Yes, it makes no difference using that command with 0 or 1 for optimize. So I made some tests and see that AD needs round about 5-6 seconds longer than ffmpeg with the same video file. That's of course a big difference. Maybe one of the problem is the large CLI output so how can I disable / hide the full debug / logging print in CLI window? Maybe this would it make faster. When using ffmpeg with "-loglevel debug" command it takes also few seconds longer but still not so long as AD.

PS: Can you post some links to entire commands info I can use with AD & special commands for the python script like that getVideoDuration() function so I did not find it before anywhere. Thanks.

eumagga0x2a

Quote from: Silox on February 16, 2024, 10:36:39 PMit makes no difference using that command with 0 or 1 for optimize.

You probably forgot to drop "--output-format MP4" from the command line thus overriding "optimize=0" from the script.

Quote from: Silox on February 16, 2024, 10:36:39 PMCan you post some links to entire commands info I can use with AD & special commands for the python script like that getVideoDuration() function so I did not find it before anywhere.

For the list of methods and variables available in tinyPy classes Avidemux, Editor, Gui and Tools, run e.g.

Editor.help()
in the Scripting Shell in the graphical Avidemux application.

For the list of command line options, run the CLI executable (or, on Linux or on macOS, alternatively the graphical executable in a terminal) with option --help

Silox

Thanks for that info about Editor help().

In this case of using the command "--output-format MP4" or not it makes no differences and AD still takes a long time. :( I'm using latest avidemux_r240129_win64Qt5_22).

About the command lines I get to see via --help. There is no command listed to prevent the whole logging output. How to disable the output to basic one similar ffmpeg does without print entire pages so this takes time too. Otherwise my question is whether you did some own tests maybe? Just try similar to save file in copy mode only and see how long time it takes in your case. As I said, the whole debug print in CLI window I would like to prevent if I don't need it and a quicker AD CLI variant would be welcome too you know. Maybe you can do anything about that. Thanks.

eumagga0x2a

#7
Quote from: Silox on February 17, 2024, 11:50:26 PMHow to disable the output to basic one similar ffmpeg does without print entire pages so this takes time too.

In general, simply redirect stdout to NULL:

your-command-line > NULL
More useful, redirect it to a file (e.g. to "avidemuxCliLog.txt") for later inspection:

your-command-line > "DriveLetter:\Path\to\avidemuxCliLog.txt"
Avidemux is developed and tested almost exclusively on Linux, but I've finally tested avidemux_cli.exe on Windows: On my current PC and with a pretty short sample MP4 video of ~ 60 MiB stored on a traditional rotational HDD, the time needed for "optimizing for streaming" (rewriting the entire file in-place, moving the index to the head of the file) was unexpectedly negligible, about 1/5 of a second. In this regard, you are right, very significant performance penalty from moving the index I had in mind was probably thing of the past.

The entire operation including streaming optimization took approx. 800 ms: ~30 ms for initialization (searching and loading plugins), ~100 ms for loading the file including decoding of the first 100 frames because of H.264 codec to probe for invalid PTS (ffmpeg doesn't do that, this step can take quite some time depending on video resolution and hardware performance), the script part took in this case just one millisecond, saving took ~700 ms, seeking back to start about 2 ms (ffmpeg doesn't do that, it is useless for a CLI app, but necessary for the graphical one, it might be worth looking for a way to separate code paths in Avidemux).

The initialization phase took very short time due to a fast SSD and to Avidemux having been already used in the current Windows session. It might take longer on a rotational HDD and when Avidemux runs for the first time.

Letting avidemux_cli.exe dump its debug output into cmd.exe terminal window prolonged the time needed for the same operation to approx 1800 ms.

I am not sure why this really matters. The CLI is used mostly for unattended processing, most exclusively for re-encoding, when slowdown due to stdout being connected to terminal is irrelevant - the encoder becomes the bottleneck.

addendum: tested with my local cross-compiled build off the latest git master.

eumagga0x2a

As pointed out by szlldm, stream copy taking longer in Avidemux than in ffmpeg can be most likely attributed to the checks Avidemux performs while loading MP4 or MKV source videos with codecs other than VP9 and AV1 (it decodes frame headers to determine frame type, not relying on PTS and flags provided by container).

There would hardly be any difference when loading pre-indexed MPEG-TS.

Silox

Hi eumagga0x2a,

thank you for doing some tests. So I see that it now works much super faster without the terminal debug print using the > file command. :) That's all as I thought already. Just don't know why it takes so much longer time with the debug print in terminal but I did notice that already before with other apps too. Only issue I have now is whether you could add a command in next release to prevent creating any debug print. Just the basic print info...
Encoding Phase        : Saving
Encoding Audio codec  : AAC
Encoding Container    : MP4
...in terminal is enough without to output all other info's into extra file you know. I mean I don't want create an external debug file and even not in terminal window itself only basic. Just if possible and you don't mind.

I have another question about AD. I also did notice when I do some keyframe cut that I got some harsh audio crackle issues right at the beginning of the new created video file. Also inside of AD the audio is not playing clean at the beginning of any pointer position too (mostly) etc. Maybe you could check this problem out and trying to fix it to keep the audio playback correctly without any issues inside of AD and in output files. If you want I can show you the differences of original A/V part and the keyframe output A/V part as wave image so there you can also see it. By the way, a visible audio wave form in AD would be very helpfully to set better cut points during video editing you know. I also see that you didn't add an video close button yet (strg+W) next to info or filter button and a full screen option is still not supported too yet which you could implement (as simple double click on video screen). All things I would add to make editing more comfortable and more enjoyable. Thanks.

eumagga0x2a

Quote from: Silox on February 19, 2024, 12:31:59 AMAlso inside of AD the audio is not playing clean at the beginning of any pointer position too (mostly) etc.

Might be either specific to Win32 audio device or to a specific codec, I don't experience this issue with PulseAudio (Linux) or CoreAudio (macOS) and also haven't noticed it while briefly testing on Windows. Would you please provide more details regarding codec? Can you reproduce it with almost every video? Else please provide a sample.

A perceptible slowdown from stdout being connected to cmd.exe console window may occur only in very special use cases which trigger high frequency of debug messages, but in general, always outputting debug messages is problematic, sure (it has been always this way in Avidemux...).

Silox

Hi again,

I think I found the problem. Seems that AD has some 1 frame forwarding problem. In clear text it means the AD user don't get always the same result out as in AD when the user did edit the video content. I have created 2 test videos and each one has a duration of 5 seconds. The one video has just noises and the other one just silence and every second I did set an key frame. When I merge them together via AD (append) and save it as new file and load this again in AD = 10 seconds long. Now I step forward to second 5 where the silence part starts and press play and AD keeps quiet and plays no sound and in first view it looks correctly. Now I just output the silence part from seconds 5 key frame via copy mode and when I play this file in VLC player then I can hear a noise sound with a length of one frame. When I load this file in VirtualDub2 then I can see that first frame with noises (audio wave form AD does still not have). When I load this file in AD then its not playing the first frame = I don't hear any noise but its there = AD does forward the video with one frame = exactly cutting gets problematically you know. I would like to post some images for you so that you can see it so I did add a visual spectrum of the audio in the video but where to upload? I will see to find any host to show you what I mean.

PS: I think this one frame forwarding issue just depends the audio only. In AD itself it's forwarding one frame in preview and in output file I get the last frame included of the not selected area. Ok, sounds pretty strange I know but I think it's better to show it with some images.

PS2: Would be nice if you could add an frame counter in AD to see & know on which frame the pointer is. Also an frame jumper instead of time code would be also nice. Quick paste of time stamps / frame would be also cool. Maybe all thing you don't like or otherwise you had add them already like a full screen switch. Man man man du!  :)

Silox

Hi again,

I made a image with some description about the problem. Just have a look. In the image I made you can see a example video I made which has 5 seconds noises and 5 seconds silence. I also did add an frame counter to know on which frame we are. In first part of image you can see the loaded video in ADM and I stopped it middle (5s) where a key frame is at frame 125. Also you can see the line in the video what is the noise. When I press play in ADM then I can not hear any sound / noise at frame 125 but the noise is there but ADM is not playing it.

On second image part I did load same file in VD2 also going to frame 125 and in the wave form you can see the noise audio and when I press play then I can also hear it. Just a short noise.

On third image part we have same situation like on first image but this time I did just step back one frame only to frame 124 and when I press play in ADM then it will play the noise frame which should be playing at frame 125.

So what does it mean? It means that ADM somehow does forward one audio frame more than the video frame you can see. But only in preview mode! Lets say I have a video with 10 frames & 10 audio frames then it would look like this (normally)...
V: 1 2 3 4 5 6 7 8 9 10
A: 1 2 3 4 5 6 7 8 9 10
...but in case of ADM preview its like this...
V: 1 2 3 4 5 6 7 8 9 10
A: 2 3 4 5 6 7 8 9 10 11
...some shifting of one audio frame. But as I said, just in preview mode only. In this case you will get inaccurate results when you edit your footage even when its only one audio frame and you don't get this out what you did edit in ADM for 100% you know. In this small example I get that noise frame not to hear in ADM preview but I have it in output / saved file = Problem. Only way to get the right preview to hear in ADM is to set the audio delay to ~40 ms and then the audio preview is fine but I have to disable it again when I edit / cut / save the video. Why does the audio don't match to video in preview mode by default (disabled audio delay)?

Silox

@eumagga0x2a

I made some more tests and found those unclean audio crack problem also sometimes when I edit a video in ADM and save it as new file (encoding). Right on those cut points I have them which is super bad of course. So my suggestion is that you do some own tests to find the and fix the problem with the unclean saved / encode audio streams in output file. Also you should add an audio wave form in ADM so that we can see the entire audio which brings just advantages for us during video editing & checking. Just only a suggestion like others I told you before a long while ago but you just seems to be really resistant to advice, unfortunately.

eumagga0x2a

If there is disagreement greater than 20 ms in audio sync between Avidemux and all or almost all popular video players, eventually different with different container formats, this needs to be investigated. A big problem might be Avidemux using the obsolete Win32 audio device.

Doing anything about it needs something I don't have at the moment – time.