视频格式转换mp4

时间:2024-01-31 16:56:54

第一步:https://ffmpeg.zeranoe.com/builds/下载ffmpeg 

或者:百度云下载:

链接:https://pan.baidu.com/s/1x_QogbV8xFjkYTeDpB1LVA
提取码:w04r 

 

第二部:解压并复制改目录的ffmpeg.exe文件路径

 

# -*- coding: utf-8 -*-
#!/usr/local/bin/python
# Time: 2017/8/30 22:17:47
# Description:
# File Name: movie2mp4.py

import os
import subprocess
import time
import logging


logger = logging.getLogger()
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
fh = logging.FileHandler(filename="./convert.log")
formatter = logging.Formatter(
    "%(asctime)s - %(name)s - line:%(lineno)d - %(levelname)s - %(message)s"
)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(ch)  # 将日志输出至屏幕
logger.addHandler(fh)  # 将日志输出至文件

movie_path = r"C:\Users\user\Desktop\chaos"
movie_type = [".mkv", ".rmvb", ".avi",".flv",".MKV", ".RMVB", ".AVI",".FLV"]

for root, dir, files in os.walk(movie_path):
    for file in files:
        # print(file)
        shotname, extension = os.path.splitext(file)
        # print(shotname,extension)
        source_name = os.path.join(root, file)
        target_name = os.path.join(root, f"{shotname}.mp4")
        if extension in movie_type and not os.path.exists(target_name):
            logger.info(f"{source_name} -> {target_name}")
            bin_path = (
                r"c:\Users\user\Desktop\ffmpeg-20200225-36451f9-win64-static\ffmpeg-20200225-36451f9-win64-static\bin\ffmpeg.exe"
            )
            #command = f"{bin_path} -i {source_name} -strict -2 {target_name}"
            command = f"{bin_path} -i {source_name} -c:v copy -c:a copy {target_name}"
            logger.info(command)
            try:
                a = subprocess.run(command, shell=True)
                if a.returncode == 0:
                    logger.info("convert successful.")
                    b = subprocess.run(f"del {source_name}", shell=True)
                    if b.returncode == 0:
                        logger.info("delete source successful.")
            except Exception as e:
                logger.info(f"conver error :{e}")

  

第三步:python打开改代码并把需要替换视频的储存的路径替换move_path

第二步复制的ffmpeg.exe的路径替换bin_path

 

第四步:执行代码

ok