使用ffmpeg来合成

import subprocess
import json
import time
import os

def run(video_path, audio_path, mp4_path):
    mp4_path = mp4_path.replace(" ", '_')
    cmd_ = "ffmpeg -i " + video_path + " -i " + audio_path + " -codec copy " + mp4_path
    print(cmd_)
    sub = subprocess.Popen(cmd_, shell=True, stdout=subprocess.PIPE)
    result = sub.stdout.read()
    
if __name__ == '__main__':
    file_dir = r"E:\output"  # 需要合并以及输出的文件路径
    for root, dirs, files in os.walk(file_dir):
        if len(dirs) == 0:
            video_path = os.path.join(root, "video.m4s")  # 获取到视频数据流
            audio_path = os.path.join(root, "audio.m4s")  # 获取到音频数据流
            json_path = os.path.join(os.path.dirname(root), "entry.json")  # 获取到json数据信息
            if os.path.exists(json_path):
                with open(json_path, 'r', encoding="utf-8") as f:
                    data = f.readlines()[0]  # json文件,根据需要进行预处理
                try:
                    json_data = json.loads(data)
                    mp4_name = json_data['page_data']['part']  # type:str #获取文章标题
                except Exception as e:
                    print(e, "文章标配获取失败", data, root)
                    mp4_name = "error-" + time.strftime('%Y-%m-%d %H%M%S', time.localtime())
                finally:
                    out_path = mp4_name.split('-')[0]  # 按照标题-分割进行分类
                    if not os.path.exists(os.path.join(file_dir, out_path)):
                        os.mkdir(os.path.join(file_dir, out_path))
                    mp4_path = os.path.join(file_dir, out_path, mp4_name) + ".mp4"
                    if os.path.exists(video_path) and os.path.exists(audio_path):
                        print("查找成功")
                        run(video_path=video_path, audio_path=audio_path, mp4_path=mp4_path)

运行结束,还有待补充:

1.ffmpeg在pycharm运行失败的解决方法

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐