Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Written by Khalid Shahin - July 24, 2022
- #YouTube channel: https://www.youtube.com/c/KhalidSMShahin
- #email: khalid@evidence.report
- import sys
- import csv
- import math
- framerate = 23.976
- files = ["frameio_export.csv"] #The default file to load, this will be replaced if you dragged and dropped files onto the script, or ran this via command line with arguments
- if len(sys.argv) > 1: #If files were dragged and dropped onto the program
- files = sys.argv[1:]
- def addLeadingZeroes(number, numberOfDigits):
- if len(str(number)) > numberOfDigits:
- #return str(number)
- return "000"
- else:
- return "0"*(numberOfDigits-len(str(number))) + str(number)
- for filename in files:
- rows = []
- outputrows = []
- with open(filename, newline='') as csvfile:
- reader = csv.reader(csvfile, delimiter=',', quotechar='"')
- for row in reader:
- rows.append(row)
- headers = rows[0]
- print(headers)
- timeStamps = []
- timeCodes = []
- for rowIndex in range(len(rows)):
- row = rows[rowIndex]
- if rowIndex > 0:
- comment = row[headers.index("Comment")]
- while comment[0] == '"' and comment[-1] == '"':
- comment = comment[1:-1]
- #print(comment)
- if row[headers.index("Commenter")]:
- commenter = row[headers.index("Commenter")]
- while commenter[0] == '"' and commenter[-1] == '"':
- commenter = commenter[1:-1]
- commenterAndComment = commenter + ": " + comment
- frame = row[headers.index("Frame")]
- #duration = row[headers.index("Duration")]
- timecode = row[headers.index("Timecode")]
- #timeout = row[headers.index("Time Out")]
- if timecode in timeCodes:
- startingTimeCode = timecode[-2:]
- directionChange = 1
- while timecode in timeCodes:
- if float(timecode[-2:]) < math.floor(framerate):
- trailing = str(int(timecode[-2:])+(1*directionChange))
- else:
- trailing = str(int(startingTimeCode)-1)
- directionChange = -1
- trailing = addLeadingZeroes(trailing, 2)
- timecode = timecode[:-3] + ":" + trailing
- if timecode not in timeCodes:
- timeCodes.append(timecode)
- break
- else:
- timeCodes.append(timecode)
- if float(timecode[-2:]) <= framerate:
- trailingFrameNumber = str(int((1000/framerate)*float(timecode[-2:])))
- trailingFrameNumber = addLeadingZeroes(trailingFrameNumber, 3)
- timecodeConverted = timecode[:-3] + "." + trailingFrameNumber
- outputrows.append([timecodeConverted, "", commenterAndComment])
- else:
- print("Unexpected frame input")
- for x in range(len(outputrows)):
- if x < len(outputrows)-1:
- outputrows[x][1] = outputrows[x+1][0]
- else:
- #Last row, for now I'm going to have the same value for out as in, maybe that will be enough
- outputrows[x][1] = outputrows[x][0]
- pass
- #print(outputrows)
- f = open(filename[:-4]+"_converted"+".csv", 'w')
- writer = csv.writer(f, delimiter=',', lineterminator='\n', quotechar='"', quoting=csv.QUOTE_ALL)
- for row in outputrows:
- writer.writerow(row)
- f.close()
Add Comment
Please, Sign In to add comment