所以我在我的脚本中这样做:
import json
info = json.loads(get_info())
print info
哪个输出:
richard@richard-desktop:~/projects/hello-python$ python main.py
{
"streams": [
{
"index": 0,
"codec_name": "mpeg2video",
"codec_long_name": "MPEG-2 video",
"codec_type": "video",
"codec_time_base": "1001/48000",
"codec_tag_string": "[0][0][0][0]",
"codec_tag": "0x0000",
"width": 1920,
"height": 1080,
"has_b_frames": 1,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "16:9",
"pix_fmt": "yuv422p",
"level": 2,
"timecode": "00:59:59:00",
"id": "0x1e0",
"r_frame_rate": "24000/1001",
"avg_frame_rate": "10000/417",
"time_base": "1/90000",
"start_time": "0.945411"
},
{
"index": 1,
"codec_name": "pcm_dvd",
"codec_long_name": "PCM signed 20|24-bit big-endian",
"codec_type": "audio",
"codec_time_base": "1/48000",
"codec_tag_string": "[0][0][0][0]",
"codec_tag": "0x0000",
"sample_fmt": "s32",
"sample_rate": "48000",
"channels": 2,
"bits_per_sample": 0,
"id": "0xa0",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"start_time": "0.945411",
"duration": "600.595000"
}
],
"format": {
"filename": "/home/richard/projects/hello-python/tests/test_1.mpg",
"nb_streams": 2,
"format_name": "mpeg",
"format_long_name": "MPEG-PS format",
"start_time": "0.945411",
"duration": "600.595000",
"size": "4033241092",
"bit_rate": "53723272"
}
}
如何访问流或格式,然后访问其中的属性?例如如何从第二个流中获取 codec_long_name 或如何从格式中获取持续时间?
我试过:
print info[0]
哪个输出:
richard@richard-desktop:~/projects/hello-python$ python main.py
{
如果我打印 repr(info),我会得到:
richard@richard-desktop:~/projects/hello-python$ python main.py
'{\n "streams": [\n {\n "index": 0,\n "codec_name": "mpeg2video",\n "codec_long_name": "MPEG-2 video",\n "codec_type": "video",\n "codec_time_base": "1001/48000",\n "codec_tag_string": "[0][0][0][0]",\n "codec_tag": "0x0000",\n "width": 1920,\n "height": 1080,\n "has_b_frames": 1,\n "sample_aspect_ratio": "1:1",\n "display_aspect_ratio": "16:9",\n "pix_fmt": "yuv422p",\n "level": 2,\n "timecode": "00:59:59:00",\n "id": "0x1e0",\n "r_frame_rate": "24000/1001",\n "avg_frame_rate": "10000/417",\n "time_base": "1/90000",\n "start_time": "0.945411"\n },\n {\n "index": 1,\n "codec_name": "pcm_dvd",\n "codec_long_name": "PCM signed 20|24-bit big-endian",\n "codec_type": "audio",\n "codec_time_base": "1/48000",\n "codec_tag_string": "[0][0][0][0]",\n "codec_tag": "0x0000",\n "sample_fmt": "s32",\n "sample_rate": "48000",\n "channels": 2,\n "bits_per_sample": 0,\n "id": "0xa0",\n "r_frame_rate": "0/0",\n "avg_frame_rate": "0/0",\n "time_base": "1/90000",\n "start_time": "0.945411",\n "duration": "600.595000"\n }\n ],\n "format": {\n "filename": "/home/richard/projects/hello-python/tests/test_1.mpg",\n "nb_streams": 2,\n "format_name": "mpeg",\n "format_long_name": "MPEG-PS format",\n "start_time": "0.945411",\n "duration": "600.595000",\n "size": "4033241092",\n "bit_rate": "53723272"\n }\n}\n'
我获取 JSON 字符串的方式是运行以下命令:
ffmpeg_command = "ffprobe -v quiet -print_format json -show_format -show_streams %s" % self.absolute_path
return subprocess.check_output(ffmpeg_command, shell=True)