Python 调用 FFmpeg 处理合并视频文件

FFmpeg 是一个开源的多媒体框架,它包含了用于处理音频、视频、字幕等多媒体数据的一系列工具、库和软件包。FFmpeg 可以执行多种多媒体处理任务,包括转码、剪辑、合并、分离、编解码、流媒体传输等。它被广泛用于多媒体应用程序和流媒体平台中,是一个功能强大且高度可定制的工具。本文主要介绍Python 调用 ffmpeg 命令处理视频文件的方法,以及相关的示例代码。

1、FFmpeg 的下载安装

FFmpeg 是一个跨平台的多媒体处理框架,它支持多种操作系统,具体安装方法可以参考下面文档,

参考文档安装 FFmpeg

2、判断是否安装 FFmpeg

import subprocess
import logging

try:
    from subprocess import DEVNULL
except ImportError:
    # Python 3.2 or below
    import os
    import atexit
    DEVNULL = os.open(os.devnull, os.O_RDWR)
    atexit.register(lambda fd: os.close(fd), DEVNULL)

def get_usable_ffmpeg(cmd):
    try:
        p = subprocess.Popen([cmd, '-version'], stdin=DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        vers = str(out, 'utf-8').split('\n')[0].split()
        assert (vers[0] == 'ffmpeg' and vers[2][0] > '0') or (vers[0] == 'avconv')
        try:
            v = vers[2][1:] if vers[2][0] == 'n' else vers[2]
            version = [int(i) for i in v.split('.')]
        except:
            version = [1, 0]
        return cmd, 'ffprobe', version
    except:
        return None

FFMPEG, FFPROBE, FFMPEG_VERSION = get_usable_ffmpeg('ffmpeg') or get_usable_ffmpeg('avconv') or (None, None, None)
if logging.getLogger().isEnabledFor(logging.DEBUG):
    LOGLEVEL = ['-loglevel', 'info']
    STDIN = None
else:
    LOGLEVEL = ['-loglevel', 'quiet']
    STDIN = DEVNULL

def has_ffmpeg_installed():
    return FFMPEG is not None
    
print("是否安装:%s"%has_ffmpeg_installed())

3、合并视频文件

一般在网络在线播放视频,需要分段传输,下载时需要将多个文件合成视频文件,就可以使用此命令,如下,

import logging
import os
import subprocess
import sys

try:
    from subprocess import DEVNULL
except ImportError:
    # Python 3.2 or below
    import os
    import atexit
    DEVNULL = os.open(os.devnull, os.O_RDWR)
    atexit.register(lambda fd: os.close(fd), DEVNULL)

def get_usable_ffmpeg(cmd):
    try:
        p = subprocess.Popen([cmd, '-version'], stdin=DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        vers = str(out, 'utf-8').split('\n')[0].split()
        assert (vers[0] == 'ffmpeg' and vers[2][0] > '0') or (vers[0] == 'avconv')
        try:
            v = vers[2][1:] if vers[2][0] == 'n' else vers[2]
            version = [int(i) for i in v.split('.')]
        except:
            version = [1, 0]
        return cmd, 'ffprobe', version
    except:
        return None

FFMPEG, FFPROBE, FFMPEG_VERSION = get_usable_ffmpeg('ffmpeg') or get_usable_ffmpeg('avconv') or (None, None, None)


def ffmpeg_concat_av(files, output, ext):
    print(DEVNULL)
    print('Merging video parts... ', end="", flush=True)
    
    params = [FFMPEG]
    for file in files:
        if os.path.isfile(file): params.extend(['-i', file])
    params.extend(['-c', 'copy'])
    params.extend(['--', output])
    if subprocess.call(params, stdin=DEVNULL):
        print('Merging without re-encode failed.\nTry again re-encoding audio... ', end="", flush=True)
        try: os.remove(output)
        except FileNotFoundError: pass
        params = [FFMPEG] + LOGLEVEL
        for file in files:
            if os.path.isfile(file): params.extend(['-i', file])
        params.extend(['-c:v', 'copy'])
        if ext == 'mp4':
            params.extend(['-c:a', 'aac'])
            params.extend(['-strict', 'experimental'])
        elif ext == 'webm':
            params.extend(['-c:a', 'opus'])
        params.extend(['--', output])
        return subprocess.call(params, stdin=DEVNULL)
    else:
        return 0

ffmpeg_concat_av(["./1.ts","./2.ts","./3.ts","./4.ts","./5.ts"],"./output.mp4","mp4")

推荐阅读
cjavapy编程之路首页