python
opencv
ffmpeg
python的相關包
import cv2, subprocess, os, shutil from moviepy.editor
import AudioFileClip
# encoding=utf8
from sys import path
import cv2, subprocess, os, shutil
from moviepy.editor import AudioFileClip
from_path = 'D:/file/'
target_path = 'D:/file/target/'
mid_flv = "mid.flv"
mid_avi = "mid.avi"
def video_format(video_name):
videoCapture = cv2.VideoCapture(target_path + video_name)
# 獲得碼率及尺寸
fps = videoCapture.get(cv2.CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 指定寫視頻的格式, I420-avi, MJPG-mp40
outfile_name = video_name.split('.')[0] + '.avi'
videoWriter = cv2.VideoWriter(target_path + outfile_name, cv2.VideoWriter_fourcc('F', 'L', 'V', '1'), fps, size)
# 讀幀
success, frame = videoCapture.read()
while success:
# cv2.imshow('isshow',frame) # 顯示
# cv2.waitKey(int(1000 / int(fps))) # 延遲
videoWriter.write(frame) # 寫視頻幀
success, frame = videoCapture.read() # 獲取下一幀
def autio_format(video_name):
my_audio_clip = AudioFileClip(target_path + video_name)
my_audio_clip.write_audiofile(target_path + "mid.wav")
def video_add_autio(file_name, mp3_file, new_file_name):
""" 視頻添加音頻 :param file_name: 傳入視頻文件的路徑 :param mp3_file: 傳入音頻文件的路徑 :return: """
outfile_name = new_file_name.split('.')[0] + '.avi'
print(outfile_name)
subprocess.call('ffmpeg -i ' + file_name
+ ' -i ' + mp3_file + ' -strict -2 -f avi "'
+ target_path + outfile_name+'"', shell=True)
def run():
num = 0
for root, dirs, files in os.walk(from_path):
for file in files:
if ".flv" in file:
shutil.copyfile(from_path + file, target_path + mid_flv)
num = num + 1
print("************** 正在轉換第 " + str(num) + " 個視頻 **********************")
format_video(mid_flv, file)
os.remove(target_path + mid_flv)
os.remove(target_path + mid_avi)
def format_video(video_name, file):
autio_format(video_name)
video_format(video_name)
video_add_autio(file_name=target_path + video_name, mp3_file=target_path + 'mid.wav', new_file_name=file)
run()
報錯內容如下:TemplateSyntaxError at
stay http://dblab.xmu.edu.cn/b