Rob*_*ood 7 python ffmpeg ffprobe
我一直试图永远解决这个问题(我是编程的新手),我无法弄明白.
我正在尝试构建一个将测试文件的脚本,并为我提供输出,我可以从中获取"音频格式"等信息,然后我可以将其放入文件名中.但是,我甚至无法让脚本返回任何文件信息.插入输入文件时我碰到了墙...
所以在这一点上我只需要帮助让它根据我投入的argv吐出信息.希望我能够弄清楚如何从中解析音频信息.
我的尝试似乎很接近:
#!/usr/bin/python
import os, sys, subprocess, shlex, re
from subprocess import call
def probe_file(filename):
p = subprocess.Popen(['/opt/local/bin/ffprobe', '-show_format', '-pretty', '-loglevel quiet', -i filename], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print filename
print p.communicate()
[probe_file (f) for f in os.listdir('.') if not f.startswith('.')]
Run Code Online (Sandbox Code Playgroud)
您的代码中几乎没有问题
-i filename这是一个语法错误使用'-i '+filename,而不是shell=True 通常不需要,是不必要的负担.除了它似乎工作,你修复#1后没有看到输出?
编辑:看起来你遇到了ffprobe命令行的问题,所以我安装了它并且你需要的更改
-i标志,输入文件只是作为最后一个参数传递.-loglevel和选择logleve quiet作为单独的参数,即[..., '-loglevel', 'quiet',..]所以这些更改之后是一个示例脚本
#!/usr/bin/python
import os, sys, subprocess, shlex, re
from subprocess import call
def probe_file(filename):
cmnd = ['ffprobe', '-show_format', '-pretty', '-loglevel', 'quiet', filename]
p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print filename
out, err = p.communicate()
print "==========output=========="
print out
if err:
print "========= error ========"
print err
probe_file('drop.avi')
Run Code Online (Sandbox Code Playgroud)
我看到了正确的输出:
==========output==========
[FORMAT]
filename=drop.avi
nb_streams=1
format_name=avi
format_long_name=AVI format
start_time=0:00:00.000000
duration=0:00:06.066667
size=660.000 Kibyte
bit_rate=891.217 Kbit/s
[/FORMAT]
========= error ========
ffprobe version 0.7.3-4:0.7.3-0ubuntu0.11.10.1, Copyright (c) 2007-2011 the Libav developers
built on Jan 4 2012 16:08:51 with gcc 4.6.1
configuration: --extra-version='4:0.7.3-0ubuntu0.11.10.1' --arch=amd64 --prefix=/usr --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --enable-libvpx --enable-runtime-cpudetect --enable-vaapi --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
libavutil 51. 7. 0 / 51. 7. 0
libavcodec 53. 6. 0 / 53. 6. 0
libavformat 53. 3. 0 / 53. 3. 0
libavdevice 53. 0. 0 / 53. 0. 0
libavfilter 2. 4. 0 / 2. 4. 0
libswscale 2. 0. 0 / 2. 0. 0
libpostproc 52. 0. 0 / 52. 0. 0
Unsupported codec with id 114 for input stream 0
Run Code Online (Sandbox Code Playgroud)
json 输出ffprobe非常简单,可以在 python 中解析,只需安装第三方库。
#!/usr/bin/env python
import argparse
import json
import subprocess
import sys
from pathlib import Path
from typing import NamedTuple
class FFProbeResult(NamedTuple):
return_code: int
json: str
error: str
def ffprobe(file_path) -> FFProbeResult:
command_array = ["ffprobe",
"-v", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
file_path]
result = subprocess.run(command_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
return FFProbeResult(return_code=result.returncode,
json=result.stdout,
error=result.stderr)
Run Code Online (Sandbox Code Playgroud)
示例用法,回显生成的 json 字符串:
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='View ffprobe output')
parser.add_argument('-i', '--input', help='File Name', required=True)
args = parser.parse_args()
if not Path(args.input).is_file():
print("could not read file: " + args.input)
exit(1)
print('File: {}'.format(args.input))
ffprobe_result = ffprobe(file_path=args.input)
if ffprobe_result.return_code == 0:
# Print the raw json string
print(ffprobe_result.json)
# or print a summary of each stream
d = json.loads(ffprobe_result.json)
streams = d.get("streams", [])
for stream in streams:
print(f'{stream.get("codec_type", "unknown")}: {stream.get("codec_long_name")}')
else:
print("ERROR")
print(ffprobe_result.error, file=sys.stderr)
Run Code Online (Sandbox Code Playgroud)
所以:
$ ./ffprobe.py -i input.mp4
File: input.mp4
Run Code Online (Sandbox Code Playgroud)
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_time_base": "1001/48000",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 1920,
"height": 1080,
"coded_width": 1920,
"coded_height": 1088,
"has_b_frames": 2,
"pix_fmt": "yuv420p",
"level": 40,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "24000/1001",
"avg_frame_rate": "24000/1001",
"time_base": "1/24000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 442440,
"duration": "18.435000",
"bit_rate": "4970031",
"bits_per_raw_sample": "8",
"nb_frames": "442",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2020-05-20T15:37:59.000000Z",
"language": "und",
"handler_name": "L-SMASH Video Handler",
"encoder": "AVC Coding"
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_time_base": "1/48000",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "48000",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/48000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 885760,
"duration": "18.453333",
"bit_rate": "137011",
"max_bit_rate": "140304",
"nb_frames": "865",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2020-05-20T15:37:59.000000Z",
"language": "und",
"handler_name": "L-SMASH Audio Handler"
}
}
],
"format": {
"filename": "input.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "18.453333",
"size": "11779747",
"bit_rate": "5106826",
"probe_score": 100,
"tags": {
"major_brand": "mp42",
"minor_version": "0",
"compatible_brands": "mp42mp41isomavc1",
"creation_time": "2020-05-20T15:37:59.000000Z"
}
}
}
video: H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
audio: AAC (Advanced Audio Coding)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12519 次 |
| 最近记录: |