How to improve mp4 resize py script to batch script

Started by avilon, November 03, 2019, 11:01:05 AM

Previous topic - Next topic

avilon

Avidemux 2.7.0, Windows 7.
Purpose is to create a script with single action to resize (increase) all files in folder (folder content is only mp4 files) to height 480 (x264) and save in different folder (or same folder with _480 appended to the end of original file name). Unfortunately I didn't find any easy to understand instructions.

I created py script with Avidemux "Save Project tool" with first mp4 in this folder.
When script is run Rezise parameter has been set to height 480, as expected.
Still manual "open" and "save as" is required on each single file.
But how to batch run this script on all files on folder?

Script I have so far:
Except row: adm.addVideoFilter("swscale", "width=852", "height=480", "algo=2", "sourceAR=1", "targetAR=1")
below all are Avidemux default settings not changed.

#PY  <- Needed to identify
#
#--automatically built--

adm = Avidemux()
adm.videoCodec("x264", "useAdvancedConfiguration=True", "general.params=AQ=20", "general.threads=99", "general.preset=", "general.tuning=", "general.profile=", "general.fast_decode=False", "general.zero_latency=False", "general.fast_first_pass=True"
, "general.blueray_compatibility=False", "general.fake_interlaced=False", "level=-1", "vui.sar_height=1", "vui.sar_width=1", "MaxRefFrames=3", "MinIdr=25", "MaxIdr=250", "i_scenecut_threshold=40", "intra_refresh=False"
, "MaxBFrame=3", "i_bframe_adaptive=1", "i_bframe_bias=0", "i_bframe_pyramid=2", "b_deblocking_filter=True", "i_deblocking_filter_alphac0=0", "i_deblocking_filter_beta=0", "cabac=True", "interlaced=False"
, "constrained_intra=False", "tff=True", "fake_interlaced=False", "analyze.b_8x8=True", "analyze.b_i4x4=True", "analyze.b_i8x8=True", "analyze.b_p8x8=True", "analyze.b_p16x16=False", "analyze.b_b16x16=False"
, "analyze.weighted_pred=2", "analyze.weighted_bipred=True", "analyze.direct_mv_pred=1", "analyze.chroma_offset=0", "analyze.me_method=1", "analyze.me_range=16", "analyze.mv_range=-1", "analyze.mv_range_thread=-1"
, "analyze.subpel_refine=7", "analyze.chroma_me=True", "analyze.mixed_references=True", "analyze.trellis=1", "analyze.psy_rd=1.000000", "analyze.psy_trellis=0.000000", "analyze.fast_pskip=True", "analyze.dct_decimate=True"
, "analyze.noise_reduction=0", "analyze.psy=True", "analyze.intra_luma=11", "analyze.inter_luma=21", "ratecontrol.rc_method=0", "ratecontrol.qp_constant=0", "ratecontrol.qp_min=10", "ratecontrol.qp_max=51"
, "ratecontrol.qp_step=4", "ratecontrol.bitrate=0", "ratecontrol.rate_tolerance=1.000000", "ratecontrol.vbv_max_bitrate=0", "ratecontrol.vbv_buffer_size=0", "ratecontrol.vbv_buffer_init=1", "ratecontrol.ip_factor=1.400000"
, "ratecontrol.pb_factor=1.300000", "ratecontrol.aq_mode=1", "ratecontrol.aq_strength=1.000000", "ratecontrol.mb_tree=True", "ratecontrol.lookahead=40")
adm.addVideoFilter("swscale", "width=852", "height=480", "algo=2", "sourceAR=1", "targetAR=1")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"unknown")
adm.audioAddTrack(0)
adm.audioCodec(0, "copy");
adm.audioSetDrc(0, 0)
adm.audioSetShift(0, 0,0)
adm.setContainer("MP4V2", "optimize=0", "add_itunes_metadata=0")

eumagga0x2a

Quote from: avilon on November 03, 2019, 11:01:05 AM
Avidemux 2.7.0

Honestly, why that old? What is the reason for missing out on over 2 years worth of bugfixes and new features? Only current nightlies and the last release (2.7.4, published as 2.7.5 for Windows) are supported on this forum.

Quote
Purpose is to create a script with single action to resize (increase) all files in folder (folder content is only mp4 files) to height 480

This is quite a problem unless you know for sure that all videos use square pixel and the same aspect ratio.

Quote(x264) and save in different folder (or same folder with _480 appended to the end of original file name). Unfortunately I didn't find any easy to understand instructions.

I created py script with Avidemux "Save Project tool" with first mp4 in this folder.
When script is run Rezise parameter has been set to height 480, as expected.
Still manual "open" and "save as" is required on each single file.

This is easy to achieve, contrary to the problem outlined above.

QuoteScript I have so far:
Except row: adm.addVideoFilter("swscale", "width=852", "height=480", "algo=2", "sourceAR=1", "targetAR=1")

This is a bad idea to specify width which is not a multiple of 16 as this impacts video compressibility. Better use "width=848".

Regarding "targetAR=1", are you sure you want this the NTSC 4:3 aspect ratio? The specified dimensions of the output picture don't match this choice.

Quoteadm.setContainer("MP4V2", "optimize=0", "add_itunes_metadata=0")

MP4v2 is deprecated (the development of the library it is based upon has stopped years ago), will result in variable frame rate when saving perfectly regular source videos and should not be used anymore. Please use the MP4 muxer which is an up-to-date libavformat (part of the FFmpeg project).


#PY  <- Needed to identify #
#
ui = Gui()
adm = Avidemux()
ext = "mp4"
#
# Function to convert an individual video
#
def convert(filein,out):
    if(0 == adm.loadVideo(filein)):
        ui.displayError("oops","cannot load "+filein)
        raise
# -------- begin project --------
    adm.videoCodec("x264", "useAdvancedConfiguration=True", "general.params=AQ=20", "general.threads=0", "general.preset=faster", "general.tuning=none", "general.profile=high", "general.fast_decode=False", "general.zero_latency=False"
, "general.fast_first_pass=True", "general.blueray_compatibility=False", "general.fake_interlaced=False", "level=-1", "vui.sar_height=1", "vui.sar_width=1", "MaxRefFrames=3", "MinIdr=25", "MaxIdr=250"
, "i_scenecut_threshold=40", "intra_refresh=False", "MaxBFrame=3", "i_bframe_adaptive=1", "i_bframe_bias=0", "i_bframe_pyramid=2", "b_deblocking_filter=True", "i_deblocking_filter_alphac0=0", "i_deblocking_filter_beta=0"
, "cabac=True", "interlaced=False", "constrained_intra=False", "tff=True", "fake_interlaced=False", "analyze.b_8x8=True", "analyze.b_i4x4=True", "analyze.b_i8x8=True", "analyze.b_p8x8=True", "analyze.b_p16x16=False"
, "analyze.b_b16x16=False", "analyze.weighted_pred=2", "analyze.weighted_bipred=True", "analyze.direct_mv_pred=1", "analyze.chroma_offset=0", "analyze.me_method=1", "analyze.me_range=16", "analyze.mv_range=-1"
, "analyze.mv_range_thread=-1", "analyze.subpel_refine=7", "analyze.chroma_me=True", "analyze.mixed_references=True", "analyze.trellis=1", "analyze.psy_rd=1.000000", "analyze.psy_trellis=0.000000", "analyze.fast_pskip=True"
, "analyze.dct_decimate=True", "analyze.noise_reduction=0", "analyze.psy=True", "analyze.intra_luma=11", "analyze.inter_luma=21", "ratecontrol.rc_method=0", "ratecontrol.qp_constant=0", "ratecontrol.qp_min=10"
, "ratecontrol.qp_max=51", "ratecontrol.qp_step=4", "ratecontrol.bitrate=0", "ratecontrol.rate_tolerance=1.000000", "ratecontrol.vbv_max_bitrate=0", "ratecontrol.vbv_buffer_size=0", "ratecontrol.vbv_buffer_init=1"
, "ratecontrol.ip_factor=1.400000", "ratecontrol.pb_factor=1.300000", "ratecontrol.aq_mode=1", "ratecontrol.aq_strength=1.000000", "ratecontrol.mb_tree=True", "ratecontrol.lookahead=40")
    adm.addVideoFilter("swscale", "width=848", "height=480", "algo=2", "sourceAR=0", "targetAR=0", "lockAR=True", "roundup=True")
    adm.audioClearTracks()
    adm.setSourceTrackLanguage(0,"und")
    adm.audioAddTrack(0)
    adm.audioCodec(0, "copy");
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0")
# --------  end project --------
    filename = basename(filein)
    separator = "\\"
    adm.save(out + separator + filename)
    print("Done")
#
# Main
#
# -------- select directory --------
inputFolder = ui.dirSelect("Select the source folder")
outputFolder = ui.dirSelect("Select the target folder")
#
list = get_folder_content(inputFolder,ext)
if(list is None):
    raise
for i in list:
    convert(i,outputFolder)
print("Done")


Taken from https://avidemux.org/smif/index.php/topic,18890.msg87638.html#msg87638, assumes a current Avidemux version.

avilon

#2
Quote from: eumagga0x2a on November 03, 2019, 03:59:59 PM
Quote from: avilon on November 03, 2019, 11:01:05 AM
Avidemux 2.7.0

Honestly, why that old? What is the reason for missing out on over 2 years worth of bugfixes and new features? Only current nightlies and the last release (2.7.4, published as 2.7.5 for Windows) are supported on this forum.

Quote from: eumagga0x2a on November 03, 2019, 03:59:59 PM
Quote
Purpose is to create a script with single action to resize (increase) all files in folder (folder content is only mp4 files) to height 480

This is quite a problem unless you know for sure that all videos use square pixel and the same aspect ratio.

Files are from different sources and so far I have resized manually by selecting height, because it's screen full heigh feels better preference 480.
I prefer also predefined height as this seems to be more important, since width may be sometimes less.
Width may vary sometimes - does this means there night be noticeable distortion of pixels?


Quote from: eumagga0x2a on November 03, 2019, 03:59:59 PM
QuoteScript I have so far:
Except row: adm.addVideoFilter("swscale", "width=852", "height=480", "algo=2", "sourceAR=1", "targetAR=1")

This is a bad idea to specify width which is not a multiple of 16 as this impacts video compressibility. Better use "width=848".
Regarding "targetAR=1", are you sure you want this the NTSC 4:3 aspect ratio? The specified dimensions of the output picture don't match this choice.

I always state height 480, 848 and multiple of 16 is defaulted by Avidemux here, is there something I should check?
I am not bonded to NTSC ratio, purpose is just to decrease file size selecting new height 480 and expecting that Avidemux sets correct width.

Have I missed some mp4 video essentials?
What is and why Avidemux has defaulted into script "targetAR=1" if incorrect?

All this script is based on action I do manually.
Open file of unknown ratio.
2. set resize to height 480
3. save this file manually.

Therefore I expected that exported script might be fine for all mp4 files.

Thanks a lot.


eumagga0x2a

Quote from: avilon on November 03, 2019, 05:44:47 PM
Files are from different sources and so far I have resized manually by selecting height, because it's screen full heigh feels better preference 480.

If the files are for home use only and not for upload to some hosting or website which requires fixed dimensions, the whole idea to rescale them is deeply flawed IMHO. It is a huge pointless waste of time and electricity only to lose video qualität in the process. Presenting videos of different dimensions in a uniform way is a classical task for a video player (for a reasonably capable video player). No quality loss, almost zero computational cost as scaling is done usually in the graphics card, the result is instantly available.

QuoteI prefer also predefined height as this seems to be more important, since width may be sometimes less.
Width may vary sometimes - does this means there night be noticeable distortion of pixels?

Depending on the source – yes, sure. Additionally, the pixel aspect ratio may be not square. You should decide based on individual video whether you want to rescale and use square pixel or to keep the storage aspect ratio of the video and force the correct display aspect ratio in the muxer (don't look for this setting in 2.7.0, all recommendations are based on a supported version).

QuoteWhat is and why Avidemux has defaulted into script "targetAR=1" if incorrect?

Maybe the semantics of targetAR parameter has changed since the old days, but to tell whether the setting is fine or not, it is necessary to know the properties of a particular source video.