使用MoviePy时Python中的意外输出

时间:2015-03-16 19:24:18

标签: python-2.7 video moviepy

您好我在此脚本中添加了一个简单的GUI

A python script to automatically summarize soccer videos based on the crowd's reactions

GUI脚本如下

from Tkinter import *

from tkFileDialog import askopenfilename
from soccer_reacts import video_edit

class MyFrame(Frame):
def __init__(self):

    master = Tk()
    Label(master, text="Please Insert Video Path With Browse", width=30).grid(row=0)
    Frame.__init__(self)
    self.master.title("Video Editor")

    self.master.geometry('{}x{}'.format(300, 200))
    self.master.rowconfigure(5, weight=1)
    self.master.columnconfigure(5, weight=1)
    self.grid(sticky=W+E+N+S)

    self.button = Button(self, text="Browse", command=self.load_file, width=15)
    self.button.grid(row=1, column=0, sticky=W)
    self.button2 = Button(self, text="Start", command=self.vid_reactions, width=15)
    self.button2.grid(row=2, column=0, sticky=W)

def load_file(self):
    fname = askopenfilename(filetypes=(("MP4 files", "*.mp4"),("All files", "*.*") ))
    if fname:
        self.fname = fname

def vid_reactions(self):
    print("[*]Starting operation")
    print("[*]File : "+self.fname)
    video_edit(self.fname)
    print("[*]Operation Finished")



 if __name__ == "__main__":
MyFrame().mainloop()

结束这是足球削减的新代码

import numpy as np # for numerical operations
from moviepy.editor import VideoFileClip, concatenate


def video_edit(file_name):
clip = VideoFileClip(file_name)
cut = lambda i: clip.audio.subclip(i,i+1).to_soundarray(fps=22000) 
volume = lambda array: np.sqrt(((1.0*array)**2).mean())
volumes = [volume(cut(i)) for i in range(0,int(clip.audio.duration-2))]
averaged_volumes = np.array([sum(volumes[i:i+10])/10
                             for i in range(len(volumes)-10)])

increases = np.diff(averaged_volumes)[:-1]>=0
decreases = np.diff(averaged_volumes)[1:]<=0
peaks_times = (increases * decreases).nonzero()[0]
peaks_vols = averaged_volumes[peaks_times]
peaks_times = peaks_times[peaks_vols>np.percentile(peaks_vols,90)]

final_times=[peaks_times[0]]
for t in peaks_times:
    if (t - final_times[-1]) < 60:
        if averaged_volumes[t] > averaged_volumes[final_times[-1]]:
            final_times[-1] = t
    else:
        final_times.append(t)

final = concatenate([clip.subclip(max(t-5,0),min(t+5, clip.duration))
                     for t in final_times])
final.to_videofile(file_name) # low quality is the default

当我运行新代码时,输​​出是一个mp4文件,带有匹配的声音 但没有视频。我检查了所做的所有更改,但我找不到任何错误。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我不知道为什么要改变最后一行 final.to_videofile(FILE_NAME) 到final.write_videofile('结果.mp4') 是一个解决方案