Skip to content

Instantly share code, notes, and snippets.

@kulmajaba
Last active August 6, 2017 19:04
Show Gist options
  • Save kulmajaba/11305dacd665943b70bdd9850552d499 to your computer and use it in GitHub Desktop.
Save kulmajaba/11305dacd665943b70bdd9850552d499 to your computer and use it in GitHub Desktop.
Convert Adobe Prelude subclip markers exported as CSV to a better format.
"""Convert subclip marker CSV file exported from Prelude to a more user-friendly format."""
import argparse
import csv
import platform
# Add your category abbreviations here
CATS = {
'ha': 'Haastis',
'fi': 'Fiilis',
'yl': 'Yleiskuva',
'to': 'Toiminta',
'yk': 'Yksityiskohta',
'il': 'Ilmakuva'
}
# Use OS-specific path delimiter
PATH_DELIMITER = '\\' if platform.system() == 'Windows' else '/'
def main():
"""Main function"""
# Argument parsing and --help feature
parser = argparse.ArgumentParser(
description="""Convert subclip marker CSV file exported from Prelude
to a more user-friendly format. USAGE: Takes in subclip markers only.
The marker name should be in format note/category/rating, delimited
by forward slashes. For the category, use abbreviations specified
in the beginning of the script file
(by default 2 first letters of the word in lowercase).""")
parser.add_argument('-d', '--depth',
dest='depth',
default=1,
type=int,
help="""Depth of path that you want to retain,
1 = only the file name itself (default)""",
metavar='depth')
parser.add_argument('inputfile',
type=str,
help='Path to the file',
metavar='inputfile')
parser.add_argument('outputfile',
type=str,
help="""Path to the output file
that will be created""", metavar='outputfile')
args = parser.parse_args()
# Initialize result table (list of lists)
table = []
with open(args.inputfile, 'r', encoding='utf-16', newline=None) as original:
reader = csv.reader((row.replace('\0', '') for row in original), delimiter='\t')
for row in reader:
# Each row from marker data is now
# [filepath, intimecode, outtimecode, category (empty), rating (empty), name]
table.append([row[9], row[4], row[5], '', '', row[0]])
original.close()
# Remove column headers, We'll add those back later
table.pop(0)
row_note = []
row_filename = []
parsed_filename = ''
for row_data in table:
row_note = row_data[5].split('/') # Split marker name to list
row_filename = row_data[0].split(PATH_DELIMITER) # Split file path to list
# Place just the file name to parsed_filename
parsed_filename = row_filename[-1]
# Iterate and add parts of path in reverse order if required by -d parameter
end = max(0, len(row_filename) - 1 - args.depth)
for i in range(len(row_filename) - 2, end, -1):
parsed_filename = row_filename[i] + PATH_DELIMITER + parsed_filename
# Final file name
row_data[0] = parsed_filename
# Category
try:
row_data[3] = CATS[row_note[1]]
except KeyError:
# Abbreviation not found in CATS
row_data[3] = row_note[1]
except IndexError:
# No category marked
pass
# Rating
try:
row_data[4] = row_note[2]
except IndexError:
pass
# Note
row_data[5] = row_note[0]
# Add column names back
table.insert(0, ['File', 'In', 'Out', 'Category', 'Rating', 'Note'])
# Write result table to a CSV file
with open(args.outputfile, 'w+', encoding='utf-16', newline='') as output:
writer = csv.writer(output, delimiter='\t')
writer.writerows(table)
output.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment