1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import cv2 from pydub import AudioSegment from moviepy.editor import * def clip_video(self): audio = AudioFileClip(self.video_file_path) audio.write_audiofile(self.folder_path+"/audio.wav") audio = AudioSegment.from_file(self.folder_path+"/audio.wav") edl_path = self.edl_file_path title = self.detect_edl_title(edl_path) title_list=title.split("_") self.data=self.extract_data() self.show = [] self.edl_infomation=[] for clip_name, timecode_in, timecode_out in self.data: hours_in, minutes_in, seconds_in, frames_in = map(int, timecode_in.split(":")) hours_out, minutes_out, seconds_out, frames_out = map(int, timecode_out.split(":")) start_time = (hours_in * 3600 + minutes_in * 60 + seconds_in) * 1000 + frames_in * (1000 / 24) end_time = (hours_out * 3600 + minutes_out * 60 + seconds_out) * 1000 + frames_out * (1000 / 24) edl_frames = (3600 * (hours_out - hours_in) + 60 * (minutes_out - minutes_in) + ( seconds_out - seconds_in)) * 24 + (frames_out - frames_in) clip_name = title_list[0] + "_" + clip_name self.edl_infomation.append([clip_name, edl_frames,start_time,end_time])
video_path = self.video_file_path video_path = video_path.replace("\\", "/").replace('"', '').replace("'", "").strip() video_capture = cv2.VideoCapture(video_path) fps = video_capture.get(cv2.CAP_PROP_FPS) width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH) height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT) frames = video_capture.get(cv2.CAP_PROP_FRAME_COUNT) size = (int(width),int(height)) fourcc = cv2.VideoWriter_fourcc('H', '2', '6', '4') start_frame=0 for item in self.edl_infomation: video_capture = cv2.VideoCapture(video_path) output_dir=self.folder_path.replace("\\","/") out_video_pth = output_dir+"/"+item[0]+"_temp_.mov" video_write = cv2.VideoWriter(out_video_pth, fourcc, fps, size, True) end_frame = start_frame+int(item[1]) pos_frame = start_frame while (pos_frame <= end_frame): video_capture.set(cv2.CAP_PROP_POS_FRAMES, pos_frame) isSuccess, frame = video_capture.read() video_write.write(frame) pos_frame = pos_frame + 1 video_capture.release() video_write.release() index=self.edl_infomation.index(item) out_video_pth = out_video_pth.replace("\\", "/").replace('"', '').replace("'", "").strip() video=VideoFileClip(out_video_pth) video_time=video.duration output_audiofile=output_dir+"/"+item[0]+".wav" audio_segment = audio[item[2]:item[3]] audio_segment.export(output_audiofile, format="wav") audiofile=AudioFileClip(output_audiofile) video_out=video.set_audio(audiofile) out_name=out_video_pth.replace(".mov",".mp4") output_path=output_dir+"/"+item[0]+".mov" video_out.write_videofile(out_name) ff="ffmpeg.exe" cmd01=ff+" -i "+out_name+" -ss 0 -vframes "+str(end_frame-start_frame)+" -y "+output_path os.system(cmd01) os.remove(out_video_pth) os.remove(out_name) os.remove(output_audiofile) start_frame=end_frame if end_frame == frames: break os.remove(self.folder_path+"/audio.wav")
|