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")