2.8.1 need help to write script in python (a kind of batch/auto cutting)

Started by avi2learn, January 22, 2024, 10:03:42 PM

Previous topic - Next topic


Hiiiii, I'm learing to write a script (also start learning python), but find some wired things. The aim of my script is to read timestamps and output file name from a csv file (which is exported from losslesscut), and then to cut and name files "automatically".
**Avidemux might support more format and convert powerfully, but no chance to multi-cut. Sometimes we can get a better version of original video, and need to re-cut it. That's why I'm thinking of write this script. ;)  ;)

1. As I searched in this forum, the script might not be able to read csv or txt files.
   So I paste the text info in the script, then read directly from string.

2. I need to spilit those text info into needed parameters, but split() function of python doesn't work as expected. For example, a string is "aaa,bbb,ccc,ddd", after splited by ",", the results are "aaa,bbb,ccc,ddd", "bbb,ccc,ddd", "ccc,ddd", "ddd".
   I solved this by using an own function.

3. As are read from string, timestamps (losslesscut use unit second, Avidemux use microsecond) need coverted from string to float. I was stopped here as float() funtion doesn't work. I add "import math". The warning saied "exception: G" (sometimes strange symbol instead of "G").

(sometimes commented lines of codes still causes error, I shall delete them...not sure if it's another wired thing)

Could you help me with that? Or it is just impossible?  :'(  :'( I didn't find much documentation about what TinyPy supports. Or is there other script language I can try?  Wish to hear any from you! Thxxxxx ;D  ;D


Regarding correct use of split() method, you might find the topic https://avidemux.org/smif/index.php/topic,20393.0.html useful as a source of inspiration.


Quote from: eumagga0x2a on January 23, 2024, 12:44:31 AMRegarding correct use of split() method, you might find the topic https://avidemux.org/smif/index.php/topic,20393.0.html useful as a source of inspiration.

Thanks @eumagga0x2a, I wrote a simple function to split timestamp and name of cut (the name sometimes is empty, and I might try later to keep "," in name as timestamp and name are separated by ","... The structure is quite simple in my case as I only need to cut them in 3 part).

The string to float (memtioned before as problem 3) is solved, I wrongly used ui.displayInfo() to show results >.<

So far, I successfully use the loop to cut a whole video by existed timestamp from .csv file. The only problem now is how to read strings directly from files.  :'(  :'(

Thanks and looking forward to further news~


Full codes to implement the function above is pasted here. Wish it helpful in some cases. I paste the time-name information from .csv file, which is exported from losslesscut (use senconds as units). It might support other software's time files in same format. If I found the way to read .csv file directly, I will update ;)  ;) 

P.s. could it be written to a plugin? I'm using Macos...for writing plugin, should I change to a Windows?

# Title:  Avidemux's batch-cutting with time file (exported by llc)
# Author:  @avi2learn  (in Avidemux Forum)
# Date:    2 Feb 2024
# Version: 1.1.2
# for learning and discusion only, editable and changeable
# please don't repost the same to other platform
# DO NOT use for any commercial purposes

# import math
adm = Avidemux()

#please paste time, names from .csv file (i.e. exported from llc) in ""
#unit [seconds], no "," in name
times_name = "520.138022,625.58368,xxxxx

#decide if auto sequencing, 1 -> yes, 0 -> 0
noFlag = 1

#auto sequencing with certain format,default: (No.)-(name)
def add2name(i, cutname):
    xname = ""
    # [i] is number,[cutname] is name
    xname = str(i) + "-" + cutname
    #FYI, example below indicates the result:【5.(No.)】(name)
    #"【5." + str(i) + "】" + cutname

    return xname

# spliter, accept empty element
def mysplit(delimiter, strng):
    A = ""
    B = []
    for i in strng:
        if i != delimiter:
            A += i
            A = ""
    # Append last word


#choose a video file to cut. if prefer drag in UI, delete the following 3 lines
inFilepath = gui.fileReadSelect("Select input file")
if not adm.loadVideo(inFilepath):

#choose a folder to save output cuts. if always same folder, delete following 5 lines and type: outdir = "(文件夹路径)", no "/" at end
    outdir=gui.dirSelect("Select the target folder")
    gui.displayError("No output folder selected", "bye");

#default parameters,default output as .mp4
adm.setHDRConfig(1, 1, 1, 1, 0)
if adm.audioTotalTracksCount() <= 0:
    raise("Cannot add audio track 0, total tracks: " + str(adm.audioTotalTracksCount()))
adm.audioCodec(0, "copy")
adm.audioSetDrc2(0, 0, 1, 0.001, 0.2, 1, 2, -12)
adm.audioSetEq(0, 0, 0, 0, 0, 880, 5000)
adm.audioSetChannelGains(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
adm.audioSetChannelDelays(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
adm.audioSetChannelRemap(0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8)
adm.audioSetShift(0, 0, 0)
adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")

lines = mysplit("\n", times_name)

i = 0
for cutline in lines:
  i += 1
  #gui.displayInfo("Cutting: " , str(i) + "-" + cutline)
  cutnow = mysplit(",", cutline)
  # start, end point
  cutStart = float(cutnow[0])*1000000;
  cutEnd = float(cutnow[1])*1000000;
  #naming content
  cutName = cutnow[2]
  # when noFlag = 1, auto sequencing, format can be changed in add2name()
  if noFlag:
      plusName = add2name(i, cutName)
      plusName = cutName
  #set markers according to start, end point
  adm.markerA = cutStart
  adm.markerB = cutEnd
  #output cuts, default as .mp4, can be changed (also with parameters above)
  fileName = outdir + "/" + plusName + ".mp4"


If you are using python (i've never used it), I recommend asking the user for the csv file and reading the csv file into the script: https://www.w3schools.com/python/python_file_handling.asp

Also, if you are using loselesscut to get the timestamps, why not just cut the video with it?