News:

--

Main Menu

Avidemux 2.6.10 python scripting

Started by DoctorNull, May 06, 2016, 04:44:03 PM

Previous topic - Next topic

DoctorNull

I am using Avidemux 2.6.10 and lately I have 2 problems with the scripting of Avidemux which is now Python based using tinypy.

I am using the following references:
http://hamaluik.com/posts/changing-the-audio-track-of-a-video-with-avidemux/
http://www.avidemux.org/admWiki/doku.php?id=tinypy:gui

1. I have written the following script (I am not proficient in Python so all comments are welcome), but it seems like that after adding the subtitles (*.ass file) to the video (encoding it), the video file size getting MUCH BIGGER than it was before. It happens mostly with *.avi files.

2. I would like to know how is it possible to do that it will encode the list of files in the selected directory, even if it contains several file's types (*.avi / *.mkv / *.mp4 etc.)

adm = Avidemux()
gui = Gui()

def convert(filein):
onlyName = basename(filein)
fileout=filein
subtitle = onlyName[:-3]
dir = dirname(filein) + "/"
subtitle = dir+subtitle+"ass"
if(0 == adm.loadVideo(filein)):
gui.displayError("ops", "cannot load "+filein)
raise
adm.videoCodec("xvid4", "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"
, "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=0", "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.setContainer("AVI", "muxerType=0", "useAlternateMp3Tag=True")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"unknown")
adm.audioAddTrack(0)
adm.audioCodec(0, "copy");
adm.audioSetDrc(0, 0)
adm.audioSetShift(0, 0,0)
adm.addVideoFilter("ssa", "font_scale=1.000000", "line_spacing=0.100000", "subtitleFile="+subtitle, "fontDirectory=c:", "extractEmbeddedFonts=1", "topMargin=0", "bottomMargin=0")
adm.addVideoFilter("ssa", "font_scale=1.000000", "line_spacing=0.100000", "subtitleFile=C:/Program Files/Avidemux 2.6 - 64 bits/credits.ass", "fontDirectory=c:", "extractEmbeddedFonts=1", "topMargin=0", "bottomMargin=0")
adm.save("C:/Users/DoctorNull/Desktop/Encoded"+onlyName)


folder = gui.dirSelect("Select the folder with the videos")
list=get_folder_content(folder,"avi")
if(list is None):
raise
for i in list:
convert(i)


EDIT:
In short:
The code takes a subtitles file called "credits" (which located inside the folder of Avidemux),
finds all *.avi files in the chosen folder by the user and checks subtitle files have identical names * .ass video files,
and encodes the subtitles (both "credits" and the matching *.ass named file) into each video file, one after one.

so, for example:
Quoteselected folder: "To Encode"
files in selected folder:
abcd.avi
abcd.ass
efgh.avi
efgh.ass
ijkl.mp4
ijkl.ass

the existing code will relate only to the first 2 files, since they are the only *.avi files.
I want it to encode ALL of the files, both *.avi and *.mp4


I hope what I have requested is possible and really appreciate any help you can provide.

Jan Gruuthuse

general.params=AQ=24 or even higher.
Use one of your avi's. Mark 10 seconds and save with different settings 24, 26, 28, ..
See what is still acceptable for you.
in avidemux QT gui this is controlled in Video Output
[Mpeg4 AVC (x264)]
[ Configure ]
[General]
Rate Control
move the slider to the right to increase the value (general.params=AQ=) and lower video quality.


Jan Gruuthuse

You do know you could remux .ass file directly into mkv. That is if your media player does support ass in subtitle track.

DoctorNull

First, I will begin by saying THANK YOU for the quick answers listed, I do not see it as obvious.

Quote from: Jan Gruuthuse on May 06, 2016, 06:42:20 PM
general.params=AQ=24 or even higher.
Use one of your avi's. Mark 10 seconds and save with different settings 24, 26, 28, ..
See what is still acceptable for you.
in avidemux QT gui this is controlled in Video Output
[Mpeg4 AVC (x264)]
[ Configure ]
[General]
Rate Control
move the slider to the right to increase the value (general.params=AQ=) and lower video quality.
I guess that this section refers to my first issue?
I'd love to keep the quality of the file as is (or at least as close as possible to the source), and still encode the subtitles.
I am able to do this with softwares like VirtualDubMod, but with the script I wrote it looks like something went wrong.
Anyway, I will try to increase slightly the general.params=AQ= parameter and get back to you with an answer.

Quote from: Jan Gruuthuse on May 06, 2016, 06:46:03 PM
Probably you're looking for this: Sample script : Convert a whole folder
If I got it right and it refers to the second issue, that would be lovely. Yet, the file seems to be missing ("404 - Attachment Not Found").
Quote from: Jan Gruuthuse on May 06, 2016, 06:48:01 PM
You do know you could remux .ass file directly into mkv. That is if your media player does support ass in subtitle track.
I could not understand this part. Would you please rephrase?

Jan Gruuthuse

#5
you now are burning subtitle into image.
some players do support subtitle files (internal or external)
mkvtoolnix can take the subtitles (*.ass file)  directly into the mkv container.
A supporting player (like vlc) allows you then to select a subtitle for display.
Could be more the one subtitle (language for expample)
---
sorry for the missing file. Was not aware these were gone.
sample script by mean. you still need to modify
- add extra loop, where you would change the variable ext with the other needed file extensions (mp4, ...)
ext="avi"

DoctorNull

Quote from: Jan Gruuthuse on May 06, 2016, 07:38:39 PM
you now are burning subtitle into image.
some players do support subtitle files (internal or external)
mkvtoolnix can take the subtitles (*.ass file)  directly into the mkv container.
A supporting player (like vlc) allows you the to select subtitle for display.
could be more the one subtitle (language for expample)

sorry for the missing file. Was not aware these were gone.
see if i can upload one
Replying for each problem mentioned in the original message (so it will be easier to follow):

1. Unfortunately, as far as I can tell, changing the general.params=AQ= parameter does not affect the encoded video file size.
Is there anything else/more that I should change in order to maintain the quality, yet reduce the video file size?

2.I will appreciate it very much if you could recover a file like that. Yet, before you do, let me just clarify:
I want that when I activate the script, it will encode ALL the video files on the selected folder, whether some of them has the same format or not (could be just *.avi but could be also some as *.avi and some as *.mp4)
It should be possible with
if filein.lower().endswith('*.avi'):
   ....
elif filein.lower().endswith('*.mkv'):
   ....
elif filein.lower().endswith('*.mp4'):
   ....

yet I am not sure where or how to do so.



As about encoding the subtitles, I know that some media-players support subtitles, as a matter of fact I use one (VLC), yet the purpose is to implement the subtitles and upload it for others to see it online.

Thanks again, and looking forward to your reply.

Jan Gruuthuse

720p video 0.01 second with
general.params=AQ=20 give me filesize of 178.5 kB
general.params=AQ=24 give me filesize of 145.9 kB
adm.videoCodec("x264", "useAdvancedConfiguration=True", "general.params=AQ=24", "general.threads=0", "general.preset=ultrafast", "general.tuning=film", "general.profile=baseline", "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=0"
, "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")

sorry not familiar any more with windows and batch programming.
I'm a user just as you are, don't have access to servers.
If you have one working for avi, at the end, copy code again:

ext="mp4"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")


need another extention:
ext="flv"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")

DoctorNull

#8
Quote from: Jan Gruuthuse on May 06, 2016, 08:11:43 PM
720p video 0.01 second with
general.params=AQ=20 give me filesize of 178.5 kB
general.params=AQ=24 give me filesize of 145.9 kB

sorry not familiar any more with windows and batch programming.
I'm a user just as you are, don't have access to servers.
1. That's a shame. I guess I'll wait for someone's else reply about this first issue?



Quote from: Jan Gruuthuse on May 06, 2016, 08:11:43 PMIf you have one working for avi, at the end, copy code again:

ext="mp4"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")


need another extention:
ext="flv"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")

2. As I wrote at the beginning, the code works! IT DOES WORK! in a matter of fact, that's what the code does in short:
QuoteThe code takes a subtitles file called "credits" (which located inside the folder of Avidemux),
finds all *.avi files in the chosen folder by the user and checks subtitle files have identical names * .ass video files,
and encodes the subtitles (both "credits" and the matching *.ass named file) into each video file, one after one.

so, for example:
Quoteselected folder: "To Encode"
files in selected folder:
abcd.avi
abcd.ass
efgh.avi
efgh.ass
ijkl.mp4
ijkl.ass

the existing code will relate only to the first 2 files, since they are the only *.avi files.
I want it to encode ALL of the files, both *.avi and *.mp4
Yet, I am not sure how to fit it to run on all types of video files without creating additional python files in which you change only a few small details?
Would appreciate it if you could adjust the existing code (which appears at the top of the topic) to run on multiple formats (avi/mkv/mp4 etc.)

Jan Gruuthuse

If you have one working for avi, at the end, copy code again:

ext="mp4"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")


need another extention:
ext="flv"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")


If I'm not mistaken these are the control blocks.

Just append to the original.
1st processed are avi's, when that is finished
mp4's will be processed, when that is finished
flv's will be processed

and so for each control block you copy with other file ext

normally you would have some loop in above sample, 3 countx (x = 1, ext = avi; x = 2 , ext = mp4, x = 3, ext = flv) on each increment you would change the ext variable with an other (avi, mp4, flv, ...)

DoctorNull

Quote from: Jan Gruuthuse on May 06, 2016, 09:00:34 PM
If you have one working for avi, at the end, copy code again:

ext="mp4"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")


need another extention:
ext="flv"
if(folder is None):
    ui.displayError("oops","no folder selected")
    raise
list=get_folder_content(folder,ext)
for i in list:
        convert(i)
print("Done")


If I'm not mistaken these are the control blocks.

Just append to the original.
1st processed are avi's, when that is finished
mp4's will be processed, when that is finished
flv's will be processed

and so for each control block you copy with other file ext

normally you would have some loop in above sample, 3 countx (x = 1, ext = avi; x = 2 , ext = mp4, x = 3, ext = flv) on each increment you would change the ext variable with an other (avi, mp4, flv, ...)
Thank you!
Though, isn't the adm.videoCodec should be "xvid4" for *.avi files and "x264" for *.mp4 / *.mkv files? if so - how to make it work on the same file?

Jan Gruuthuse

That is not input, this is output codec! Avidemux detects input codecs.
All video to mkv with x264!

DoctorNull

#12
Quote from: Jan Gruuthuse on May 06, 2016, 09:17:15 PM
That is not input, this is output codec! Avidemux detects input codecs.
All video to mkv with x264!
So, basically I do not need to change any settings from AVI to MKV/MP4?
Is it okay the way it is right now as "x264" for both AVI, MKV and MP4?

EDIT: it seems like it takes longer than before now to encode MP4 video file.
The differences I've seen:








OriginalNew (Yours)
general.threads=99general.threads=0
"general.preset=""general.preset=ultrafast"
"general.tuning=""general.tuning=film"
---"general.profile=baseline"
---"general.blueray_compatibility=False"
---"general.fake_interlaced=False"


Jan Gruuthuse

script searches for video with .avi -> converts to x264 in mkv
script searches for video with .mp4 -> converts to x264 in mkv
script searches for video with .flv -> converts to x264 in mkv

xvid (x263)
adm.videoCodec("xvid4", "params=CQ=2", "profile=244", "rdMode=3", "motionEstimation=3", "cqmMode=0", "arMode=1", "maxBFrame=2", "maxKeyFrameInterval=200", "nbThreads=99", "qMin=2", "qMax=25", "rdOnBFrame=True", "hqAcPred=True"
, "optimizeChrome=True", "trellis=True", "useXvidFCC=False")


you can't mix settings form xvid with x264

DoctorNull

Quote from: Jan Gruuthuse on May 06, 2016, 09:34:25 PM
script searches for video with .avi -> converts to x264 in mkv
script searches for video with .mp4 -> converts to x264 in mkv
script searches for video with .flv -> converts to x264 in mkv

xvid (x263)
adm.videoCodec("xvid4", "params=CQ=2", "profile=244", "rdMode=3", "motionEstimation=3", "cqmMode=0", "arMode=1", "maxBFrame=2", "maxKeyFrameInterval=200", "nbThreads=99", "qMin=2", "qMax=25", "rdOnBFrame=True", "hqAcPred=True"
, "optimizeChrome=True", "trellis=True", "useXvidFCC=False")


you can't mix settings form xvid with x264
But that's not what I want. I want that:

  • if the file is MP4 it will be encoded as MP4
  • if the file is MKV it will be encoded as MKV
  • if the file is AVI it will be encoded as AVI
Is there any way to achieve that on THE SAME (ONE) SCRIPT?