opencv视频编码格式转换为H264

# coding: utf-8
import sys, os
import threading

class mp4_to_H264():
    def __init__(self):
        pass
    
    def convert_avi(self, input_file, output_file, ffmpeg_exec="ffmpeg"):
        ffmpeg = '{ffmpeg} -y -i "{infile}" -c:v libx264 -strict -2 "{outfile}"'.format(ffmpeg=ffmpeg_exec,
                                                                                        infile=input_file,
                                                                                        outfile=output_file)
        f = os.popen(ffmpeg)
        ffmpegresult = f.readline()

        # s = os.stat(output_file)
        # fsize = s.st_size

        return ffmpegresult

    def convert_avi_to_webm(self, input_file, output_file, ffmpeg_exec="ffmpeg"):
        return self.convert_avi(input_file, output_file, ffmpeg_exec="ffmpeg")

    def convert_avi_to_mp4(self, input_file, output_file, ffmpeg_exec="ffmpeg"):
        return self.convert_avi(input_file, output_file, ffmpeg_exec="ffmpeg")

    def convert_to_avcmp4(self, input_file, output_file, ffmpeg_exec="ffmpeg"):
        email = threading.Thread(target=self.convert_avi, args=(input_file, output_file, ffmpeg_exec,))
        email.start()

    def convert_byfile(self, from_path, to_path):
        if not os.path.exists(from_path):
            print("Sorry, you must create the directory for the output files first")
        if not os.path.exists(os.path.dirname(to_path)):
            os.makedirs(os.path.dirname(to_path), exist_ok=True)
        directory, file_name = os.path.split(from_path)
        raw_name, extension = os.path.splitext(file_name)
        print("Converting ", from_path)
        self.convert_avi_to_mp4(from_path, to_path)

a = mp4_to_H264()
from_path = '/home/yxq/save_mp4/(2019- 5- 28  9: 6: 0).mp4'
to_path = '/home/yxq/upload_h264/1111.mp4'
a.convert_byfile(from_path, to_path)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值