4

I am creating a Python script which uses the MoviePy module, to take clips from a larger video and concatenate them together.

The times for the clips are detailed in a CSV file like so;

0,10

11,19

15,20

34,42 etc

What I have done is read the CSV file row by row and then using the subclip method from Moviepy created a clip which is stored in a list of clips, however I get a IndexError - list index out of range.

What could be the issue (the code works fine if I don't use the subclip method with the values from the CSV file)?

This is my code:

video= VideoFileClip('file')

clipsArray = [] 

import csv
with open('csv file', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        startTime = row[0]
        endTime = row[1]
        clip = fullVideo.subclip(startTime, endTime)
        clipsArray.append(clip)

The error message is:

File "C:\Anaconda3\envs\py35\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace)

File "C:\Anaconda3\envs\py35\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "C:/spyder2-py3/program.py", line 32, in clip = fullVideo.subclip(start, end) # Create clips for each of the timestamps

File "", line 2, in subclip

File "C:\Anaconda3\envs\py35\lib\site-packages\moviepy\decorators.py", line 86, in wrapper for (arg, name) in zip(a, names)]

File "C:\Anaconda3\envs\py35\lib\site-packages\moviepy\decorators.py", line 86, in for (arg, name) in zip(a, names)]

File "C:\Anaconda3\envs\py35\lib\site-packages\moviepy\tools.py", line 78, in cvsecs finds = re.findall(expr, time)[0]

IndexError: list index out of range

CSV file:

0,12 16,21 22,29 34,59 89,130 140,160 162,171

4

1 回答 1

3

它失败的原因是因为当你从这个 csv 文件中读取时,你会得到startTimeendTime作为字符串,例如'0''12'在第一行中。

MoviePy 只接受两种时间格式:

  • 表示秒数的数字格式(int 或 float)
  • 形式的字符串'hh:mm:ss.dd'(小时、分钟、秒、秒的小数),例如'05:12:10.50'5 小时 12 分钟和 10.5 秒。

所以你应该写

startTime = float(row[0])
endTime = float(row[1])
clip = fullVideo.subclip(startTime, endTime)
于 2016-02-04T22:45:18.473 回答