I have a script that uses FFMPEG and CMD to cut video files based off of an excel document row by row. I would like python to add a timestamp after it is done with a row. Can you guys please help?
import subprocess as sp, pandas as pd
ffmpeg = 'C:/FFMPEG/bin/ffmpeg.exe' # on Windows
datafile = r'C:\Users\A_Do\Dropbox\1. Projects\2. Python\TM Creator\tm_creator_test1.xlsx'
xl = pd.ExcelFile(datafile,index = False)
df = xl.parse('Sheet1')
def create_tm():
row_iterator = df.iterrows()
# take first item from row_iterator
for i, row in row_iterator:
infile = row['filename']
outputfile = row['outputfilename']
timein = row['timein']
duration = row['duration']
decision = row['Create TM?']
if decision == "Y":
sp.call(ffmpeg + " -y -i " + infile + " -map 0:0 -map 0:1 -map 0:2 -acodec copy -ss " + str(timein) + " -codec copy -t " + str(duration) + " " + outputfile,shell=True) #this works
elif decision != decision: #this gets rid of the NaN
break
else:
print "You said you didn't want to make a TM for " + str(infile)
create_tm()
Thanks!
My final code:
import subprocess as sp, pandas as pd
# (1) new import
from openpyxl import load_workbook
# (2) new import
from datetime import datetime
ffmpeg = 'D:/FFMPEG/bin/ffmpeg.exe' # on Windows
datafile = r'D:\Dropbox\1. Projects\2. Python\TM Creator\tm_creator_test1.xlsx'
# (3) open the file in openpyxl first:
book = load_workbook(datafile)
xl = pd.ExcelFile(datafile,index = False)
df = xl.parse('Sheet1')
def create_tm():
row_iterator = df.iterrows()
# take first item from row_iterator
for i, row in row_iterator:
infile = row['filename']
outputfile = row['outputfilename']
timein = row['timein']
duration = row['duration']
decision = row['Create TM?']
if decision == "Y":
sp.call(ffmpeg + " -y -i " + infile + " -map 0:0 -map 0:1 -acodec copy -ss " + str(timein) + " -codec copy -t " + str(duration) + " " + outputfile,shell=True) #this works
# (4) Wherever in the code you want to put the timestamp:
df.loc[i, 'Timestamp'] = str(datetime.now())
# (5) This saves the sheet back into the original file, without removing
# any of the old sheets.
writer = pd.ExcelWriter(datafile)
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, index=False)
writer.save()
elif decision != decision: #this gets rid of the NaN
break
else:
print "You said you didn't want to make a TM for " + str(infile)