首页 > 技术文章 > 合成 b站视频的一段脚本

checkway 2020-08-24 21:37 原文

# -*- coding: utf-8 -*-
import json
import os
import sys
import subprocess

reload(sys)
sys.setdefaultencoding("utf8")


def read_json_file(path):
    if path is None or not os.path.exists(path):
        print ("File not exist: %s" % path)
        return None

    with open(path, 'r') as f:
        _jsonstr = f.read()
        return json.loads(_jsonstr, "utf-8")

    return None


if __name__ == '__main__':
    _work_dir = "E:/838842729"
    _ffmpegPath = "C:/Program Files (x86)/FormatFactory/ffmpeg.exe"
    _out_path = 'E:/output/'

    _files = os.listdir(_work_dir)

    for x in _files:
        _real_path = os.path.join(_work_dir, x)

        _real_json = os.path.join(_real_path, "entry.json")

        _data = read_json_file(_real_json)
        if _data is not None:
            _title = _data['page_data']['part']

            _video_path = os.path.join(_real_path, "32/video.m4s")
            _audio_path = os.path.join(_real_path, "32/audio.m4s")

            if os.path.exists(_video_path) and os.path.exists(_audio_path):
                _cmd = '"' + _ffmpegPath + '" -i "' + _video_path \
                       + '" -i "' + _audio_path + '" '
                _normal_cmd = ' -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0  '
                _out_temp = os.path.join(_out_path, 'temp.mp4')

                _cmd = _cmd + _normal_cmd + _out_temp
                if os.path.exists(_out_temp):
                    os.remove(_out_temp)

                print _cmd
                subprocess.call(_cmd, shell=True)

                if os.path.exists(_out_temp):
                    _out_file = os.path.join(_title, ".mp4")
                    os.rename(_out_temp, _out_file)

  

 

合成 b 站的 音频及视频脚本.

推荐阅读