Python将flv格式转化为avi

开发环境:

python
opencv
ffmpeg
python的相关包

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

思路

  1. 音视频分离
  2. 视频格式通过opencv转化
  3. ffmpeg将音视频合并

代码

# 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图图是只猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值