程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python converts FLV format to avi

編輯:Python

development environment :

python
opencv
ffmpeg
python The relevant package

import cv2, subprocess, os, shutil from moviepy.editor
import AudioFileClip

Ideas

  1. Audio and video separation
  2. Video format passed opencv conversion
  3. ffmpeg Merge audio and video

Code

# 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)
# Get the bit rate and size 
fps = videoCapture.get(cv2.CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# Specify the format of writing video , 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)
# Read frame 
success, frame = videoCapture.read()
while success:
# cv2.imshow('isshow',frame) # Show 
# cv2.waitKey(int(1000 / int(fps))) # Delay 
videoWriter.write(frame) # Write video frame 
success, frame = videoCapture.read() # Get the next frame 
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):
""" Video add audio :param file_name: The path to the incoming video file :param mp3_file: The path to the incoming audio 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("************** Converting page " + str(num) + " A video **********************")
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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved