[Python] export timetable clips solved

Hello,

this is my first time here, I am working on a project that require a lots of editing on big files to then create lots of clips, unfortunately Shotcut is not friendly at all about that.

Took me a couple hours to get back to Python but i’ve made a workaround.

the following Python 3.6 Script actually do the following :

  • Copy the project " *.mlt " file in RAM
  • recreate multiple copy of the same " *.mlt " but modify the Playlist Entries

It works with project that was made like the following steps :

  • Open shotcut
  • Add your video in the Playlist Section
  • add the video from Playlist -> Timetable
  • cut the part of the video that you want to keep (only one video track)
  • save the project

Python 3.6 Script Shotcut_export_timetable_as_clips.txt (3.3 KB) :

# read file
# save each line into list_original_file
# find the Playlist: starting line, ending line, number or entry
# add the Entry lines from the playlist into list_of_entry
# (List.pop() )delete the entry lines INSIDE  list_original_file <- delete items in list from line_number_start to line_number_end
# for entry in list_of_entry
# Create a new_file using <new_file_starting_name + iterator_number + new_file_extention>
# make a temporary copy of list_original_file
# List.insert(i, entry[i])
# write all lines inside the temporary copy of list_original_file inside the new_file

file_to_read = 'projet - Copie.mlt'

new_file_starting_name = 'clip_'
new_file_extention = '.mlt'

starting_line_match = '  <playlist id="playlist0">'
ending_line_match = '  </playlist>'

temp_number = 0
line_number = 0
line_number_start = 0
line_number_end = 0

list_of_entry = []
list_original_file = []
list_temporary_file = []

print('Read file')
with open(file_to_read) as search:  # read file
    for line in search:
        line_number += 1
        list_original_file.append(line) # save each line into list_original_file
        line = line.rstrip()  # remove '\n' at end of line
        if starting_line_match == line: # find the Playlist: starting line, ending line, number or entry
            #print(f'{line} at line {line_number}')
            line_number_start = line_number
        if ending_line_match == line and line_number_start != 0:
            #print(f'{line} at line {line_number}')
            line_number_end = line_number
    if line_number_start == 0 or line_number_end == 0:
        print('Playlist Not Found')
    else:
        print('Playlist Found')        
        line_number_start += 3
        line_number = 0
        with open(file_to_read) as search:
            for line in search:
                line_number += 1
                if line_number >= line_number_start and line_number < line_number_end:
                    list_of_entry.append(line) # add the Entry lines from the playlist into list_of_entry
        #print(len(list_of_entry))
        #print(len(list_original_file))
        temp_number = 0
        for entry in list_of_entry: #delete the entry lines INSIDE  list_original_file
            list_original_file.remove(entry)

        print('temporary file modified ready to create projects Clips')
        #print(len(list_original_file))

        temp_number = 1
        print('Creating projects Clips')
        for entry in list_of_entry: # for entry in list_of_entry
            filename = new_file_starting_name + str(temp_number) + new_file_extention # Create a new_file using <new_file_starting_name + iterator_number + new_file_extention>
            try:
                file = open(filename, 'r')
            except IOError:
                file = open(filename, 'w')
                list_temporary_file = list_original_file.copy() # make a temporary copy of list_original_file
                list_temporary_file.insert(line_number_start-1, list_of_entry[temp_number-1]) # List.insert(i, entry[i])
                for line in list_temporary_file:    # write all lines inside the temporary copy of list_original_file inside the new_file
                    file.write(line)
                file.close()
                temp_number +=1
                    
print('Work is finished')

You can obviously modify and reshare it to make it more user-friendly by using some “raw_input” function.

After the clips are created, you simply import (open) them into Shotcut and export as “Each Playlist Item”, careful with big video files it can overload Shotcut in RAM or just crash it (for me it get stuck at 16GB or RAM and then crash while having 32GB of RAM in total)

Some screenshots to show the result :
explorer_2018-09-07_00-13-59
from


to

Thank you for this awesome software guys :wink:

EDIT : i’ll let the moderators or admin modify my post to make the images viewable as i will probably not spend time leveling on this forum, just came here to help some. :slight_smile:

1 Like

EDIT :
After the clips are created, you simply import (open) them into Shotcut and export as timetable*, you will have to do this for each mlt clip created* ( i first though it would work to import many of them into the playlist and then “export as each playlist item” but it doesn’t work, so after all i might try to come back with a solution later)

Still saving me a nice amount of time, but definitly not what i was looking for.

PS : Thanks for the person who edited my post :wink:

This topic was automatically closed after 90 days. New replies are no longer allowed.