Avidemux Forum

Avidemux => Windows => Topic started by: wzfoto on January 21, 2020, 04:02:11 AM

Title: Batch process a bunch of short clips using tinypy
Post by: wzfoto on January 21, 2020, 04:02:11 AM
I am new to Avidemux and I m trying to write a script to process a bunch of 20-30sec clips, all shot with the same camera at the same setting.  I need to remove the audio track and cut the first and last 2-3 seconds of each clip.  I have only been able to find limited documentation online below:

https://www.avidemux.org/admWiki/doku.php?id=start

There are 2 things I m stuck on.  The container - what is the proper parameters for a mp4 container?  Whatever I have right now, results in a program crash.  The other thing is how do I cut the first and last 2 seconds of a clip? I suppose I need to use the marker function? But how do I use it? I can not find any documentation on how it works. 

Is anyone able to help?

----
def process(filePath):
    fileOutputPath = outputFolder + "\\" + basename(filePath)

    adm.loadVideo(filePath)
    adm.clearSegments()
    adm.videoCodec("Copy");
    adm.audioClearTracks()
    adm.setContainer("MP4", "muxerType=0", "useAlternateMp3Tag=False")

    adm.save(fileOutputPath);
    print("Processed file saved to (" + fileOutputPath + ")")

Title: Re: Batch process a bunch of short clips using tinypy
Post by: eumagga0x2a on January 21, 2020, 12:20:35 PM
Quote from: wzfoto on January 21, 2020, 04:02:11 AM
I am new to Avidemux and I m trying to write a script to process a bunch of 20-30sec clips, all shot with the same camera at the same setting.  I need to remove the audio track and cut the first and last 2-3 seconds of each clip.

In copy mode, a cut must begin at a GOP start, so if the first GOP is longer than 3 seconds, you won't cut anything at all.

QuoteThe container - what is the proper parameters for a mp4 container?

Load a video, select MP4 as muxer, "File" --> "Project Script" --> "Save as project". Open the generated .py file with a text editor and look for the "setContainer" call.

The configuration was changed very recently, I would recommend to use the latest nightly especially when reading from and saving to MP4.

QuoteWhatever I have right now, results in a program crash.

The crash happens because you clear all segments but don't create any new instead, not due to container choice and configuration.

QuoteThe other thing is how do I cut the first and last 2 seconds of a clip? I suppose I need to use the marker function? But how do I use it?

Right, this is where it gets complicated. Maybe


adm = Avidemux()
ed = Editor()

#######################################

def process(filePath):
    fileOutputPath = outputFolder + "\\" + basename(filePath)

    if (0 == adm.loadVideo(filePath)):
        raise
    end = ed.getVideoDuration() # duration in microseconds
    if end > 10 * 1000 * 1000:
        end -= 3 * 1000 * 1000
    start = 5 * 1000 * 1000 # 5 s
    adm.clearSegments()
    adm.addSegment(0, start, end)
    adm.videoCodec("Copy");
    adm.audioClearTracks()
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0", "clockfreq=0") # for current nightlies
    if (1 == adm.save(fileOutputPath)):
        print("Processed file saved to (" + fileOutputPath + ")")


(obviously untested).
Title: Re: Batch process a bunch of short clips using tinypy
Post by: wzfoto on January 22, 2020, 04:43:15 AM
Some good info you have provided, thank you.

However I m running into some issues.  Here is the code:

Quote
def process(filePath):
    fileOutputPath = outputFolder + "\\" + basename(filePath)

    adm.loadVideo(filePath)
    end = ed.getVideoDuration()
    if end > 9*1000*1000:
        end -= 2*1000*1000
    start = 0 #2*1000*1000

    adm.clearSegments()
    adm.addSegment(0, start, end)
    adm.videoCodec("Copy")
    adm.audioClearTracks()
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0")

    adm.save(fileOutputPath)

When I run the above, I get this error

QuoteCut 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?

After I click 'yes' it does cut the file correctly.  Any idea what the message is referring to?

However if I set the start to 2 seconds (2*1000*1000), I got the same error and upon clicking yes, will result in a crash.  From your previous you mention about needing to cut at GOP, could that be the reason for the error message?  How do I get around it so that I can cut at 2 seconds at the start?
Title: Re: Batch process a bunch of short clips using tinypy
Post by: eumagga0x2a on January 22, 2020, 08:39:53 AM
As the warning suggests, the application log (%localappdata%\avidemux\admlog.txt) has more information on the background of the issue. Please reproduce the problem and, without restarting Avidemux, compress (zip, gz or 7z) and attach this file to your reply. The main problem might be segment start not matching a frame.

In copy mode, it is absolutely mandatory for a segment to start at a GOP boundary (at a keyframe which may be an IDR or a recovery frame = seek point in an open-GOP stream), Avidemux will rewind to the previous keyframe for the start point of saving the video automatically.
Title: Re: Batch process a bunch of short clips using tinypy
Post by: wzfoto on January 23, 2020, 02:11:55 AM
See the log file attached.  It was generated by applying a script against 3 video files.  All files were processed successfully. 

If there is no way to hide the warning message, is there a way to script a click yes action against the warning message dialog box?
Title: Re: Batch process a bunch of short clips using tinypy
Post by: eumagga0x2a on January 23, 2020, 07:30:01 AM
I see, the warning is issued because the start time, set by Avidemux automatically to the last keyframe before the time specified in the project script, does not belong to any segment. You were right, setting markers is the way to go, not adding a segment shorter than the video while expecting Avidemux to start saving before the start of this segment.

Please remove

    adm.clearSegments()
    adm.addSegment(0, start, end)


from the script and replace these lines with

    adm.markerA = start
    adm.markerB = end


This should do it (please pay attention to indentation).

Quote from: wzfoto on January 23, 2020, 02:11:55 AM
If there is no way to hide the warning message, is there a way to script a click yes action against the warning message dialog box?

Choosing "no alerts" as "Message level" in the preferences?

By the way, please consider using the latest nightly, especially when you handle MP4. I would also recommend to enable DXVA2 hw accelerated video decoding.
Title: Re: Batch process a bunch of short clips using tinypy
Post by: wzfoto on January 23, 2020, 05:11:53 PM
Your suggestion to use markers works - no longer see the warning message.

And yes turning on DXVA2 made the process go much faster.

Thank you.
Title: Re: Batch process a bunch of short clips using tinypy
Post by: wzfoto on February 02, 2020, 09:38:50 PM
I was using the following script to process multiple files.  The script was working as of last night.  Today, when I use it again, I got 'Muxer, cannot open'.  Nothing has changed in terms of installation.  Not sure what the error is trying to say.  I looked into the log file (attached) and can not pinpoint where it is failing.  Any suggestions?  Kind of lost here since everything was working fine as of last night and all of a sudden its throwing meaningless errors. :(

QuotefileOutputPath = outputFolder + "\\" + basename(filePath)

    adm.loadVideo(filePath)
    end = ed.getVideoDuration()
    if end > 9*1000*1000:
        end -= 2*1000*1000
    start = 2*1000*1000

    adm.markerA = start
    adm.markerB = end

    adm.videoCodec("Copy")
    adm.audioClearTracks()
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0")

    adm.save(fileOutputPath)
    print("Processed file saved to (" + fileOutputPath + ")")
Title: Re: Batch process a bunch of short clips using tinypy
Post by: eumagga0x2a on February 02, 2020, 11:36:15 PM
The log is incomplete, cut off well before it may get interesting. When using a VC++ build of Avidemux, you should either close the application or use "Help" --> "Advanced" --> "Open Application Log" menu action to flush the buffer to disk.

By the way, I would strongly recommend a current nightly for handling mp4 files to avoid constant fps videos becoming variable fps ones due to sometimes bad choice of time base and due to rounding errors in conversion between different time bases, both issues mostly fixed recently.
Title: Re: Batch process a bunch of short clips using tinypy
Post by: wzfoto on February 09, 2020, 07:42:34 PM
Today when I try again magically everything is back to normal.  I am still clueless on what was causing the problem earlier.