Python 访问 JSON 对象中的数据

2024-02-15

所以我在我的脚本中这样做:

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 或如何从格式中获取持续时间?

I tried:

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)

JSON 已编码twice,以及结果json.loads是一个字符串。 python 中的字符串是序列,因此第一个字符是{.

再次解码该项目:

info = json.loads(json.loads(get_info()))

现在你的 main.py 输出应该如下所示:

>>> result = json.loads(output)
>>> print result
{u'streams': [{u'pix_fmt': u'yuv422p', u'index': 0, u'start_time': u'0.945411', u'codec_tag': u'0x0000', u'sample_aspect_ratio': u'1:1', u'level': 2, u'r_frame_rate': u'24000/1001', u'id': u'0x1e0', u'time_base': u'1/90000', u'codec_tag_string': u'[0][0][0][0]', u'codec_type': u'video', u'has_b_frames': 1, u'width': 1920, u'codec_long_name': u'MPEG-2 video', u'display_aspect_ratio': u'16:9', u'codec_name': u'mpeg2video', u'timecode': u'00:59:59:00', u'height': 1080, u'codec_time_base': u'1001/48000', u'avg_frame_rate': u'10000/417'}, {u'index': 1, u'sample_fmt': u's32', u'codec_tag': u'0x0000', u'bits_per_sample': 0, u'r_frame_rate': u'0/0', u'start_time': u'0.945411', u'time_base': u'1/90000', u'codec_tag_string': u'[0][0][0][0]', u'codec_type': u'audio', u'channels': 2, u'duration': u'600.595000', u'codec_long_name': u'PCM signed 20|24-bit big-endian', u'codec_name': u'pcm_dvd', u'id': u'0xa0', u'sample_rate': u'48000', u'codec_time_base': u'1/48000', u'avg_frame_rate': u'0/0'}], u'format': {u'nb_streams': 2, u'start_time': u'0.945411', u'format_long_name': u'MPEG-PS format', u'format_name': u'mpeg', u'filename': u'/home/richard/projects/hello-python/tests/test_1.mpg', u'bit_rate': u'53723272', u'duration': u'600.595000', u'size': u'4033241092'}}

您可以按名称访问流和格式参数:

>>> result['streams']
[{u'pix_fmt': u'yuv422p', u'index': 0, u'start_time': u'0.945411', u'codec_tag': u'0x0000', u'sample_aspect_ratio': u'1:1', u'level': 2, u'r_frame_rate': u'24000/1001', u'id': u'0x1e0', u'time_base': u'1/90000', u'codec_tag_string': u'[0][0][0][0]', u'codec_type': u'video', u'has_b_frames': 1, u'width': 1920, u'codec_long_name': u'MPEG-2 video', u'display_aspect_ratio': u'16:9', u'codec_name': u'mpeg2video', u'timecode': u'00:59:59:00', u'height': 1080, u'codec_time_base': u'1001/48000', u'avg_frame_rate': u'10000/417'}, {u'index': 1, u'sample_fmt': u's32', u'codec_tag': u'0x0000', u'bits_per_sample': 0, u'r_frame_rate': u'0/0', u'start_time': u'0.945411', u'time_base': u'1/90000', u'codec_tag_string': u'[0][0][0][0]', u'codec_type': u'audio', u'channels': 2, u'duration': u'600.595000', u'codec_long_name': u'PCM signed 20|24-bit big-endian', u'codec_name': u'pcm_dvd', u'id': u'0xa0', u'sample_rate': u'48000', u'codec_time_base': u'1/48000', u'avg_frame_rate': u'0/0'}]

专业提示:使用pprint module http://docs.python.org/library/pprint.html很好地格式化 python 结构:

>>> from pprint import pprint
>>> print pprint(result)
>>> pprint(result)
{u'format': {u'bit_rate': u'53723272',
             u'duration': u'600.595000',
             u'filename': u'/home/richard/projects/hello-python/tests/test_1.mpg',
             u'format_long_name': u'MPEG-PS format',
             u'format_name': u'mpeg',
             u'nb_streams': 2,
             u'size': u'4033241092',
             u'start_time': u'0.945411'},
 u'streams': [{u'avg_frame_rate': u'10000/417',
               u'codec_long_name': u'MPEG-2 video',
               u'codec_name': u'mpeg2video',
               u'codec_tag': u'0x0000',
               u'codec_tag_string': u'[0][0][0][0]',
               u'codec_time_base': u'1001/48000',
               u'codec_type': u'video',
               u'display_aspect_ratio': u'16:9',
               u'has_b_frames': 1,
               u'height': 1080,
               u'id': u'0x1e0',
               u'index': 0,
               u'level': 2,
               u'pix_fmt': u'yuv422p',
               u'r_frame_rate': u'24000/1001',
               u'sample_aspect_ratio': u'1:1',
               u'start_time': u'0.945411',
               u'time_base': u'1/90000',
               u'timecode': u'00:59:59:00',
               u'width': 1920},
              {u'avg_frame_rate': u'0/0',
               u'bits_per_sample': 0,
               u'channels': 2,
               u'codec_long_name': u'PCM signed 20|24-bit big-endian',
               u'codec_name': u'pcm_dvd',
               u'codec_tag': u'0x0000',
               u'codec_tag_string': u'[0][0][0][0]',
               u'codec_time_base': u'1/48000',
               u'codec_type': u'audio',
               u'duration': u'600.595000',
               u'id': u'0xa0',
               u'index': 1,
               u'r_frame_rate': u'0/0',
               u'sample_fmt': u's32',
               u'sample_rate': u'48000',
               u'start_time': u'0.945411',
               u'time_base': u'1/90000'}]}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 访问 JSON 对象中的数据 的相关文章

随机推荐

  • 获取redis中存在的所有哈希值

    我在 redis 缓存中有哈希值 如下所示 Hash Key Value hashme 1 Hello World hashme 2 Here Iam myhash 1 Next One 我的目标是在 CLI 中获取哈希值作为输出 如下所示
  • 无法解析符号“LocationServices”

    我正在尝试使用使用用户位置的 android studio 构建一个 android 应用程序 我正在尝试导入 google play services LocationServices api 但它说它无法解析符号 LocationSer
  • Django 表单问题:“WSGIRequest”对象没有属性“get”

    我在 Django 调试视图中收到此错误 WSGIRequest object has no attribute get 这是用于登录脚本的 其中大部分是从管理代码复制的 主要用于练习 调整原因 我在views py中的代码如下 sensi
  • 当 Firestore 文档不存在时处理“权限被拒绝”的正确方法是什么

    我已经为 Firestore 编写了规则 但是 当文档不存在时 它会抛出 权限缺失或不足 错误 例如 firebase firestore collection shipments doc order doc id onSnapshot f
  • 当键名称具有数值时,JSON.parse() 是否真的对属性进行排序?

    这里有很多关于这个问题的帖子 它们都包含很多断言 可以总结如下 永远不能保证对象属性以任何方式排序 JSON parse 从不以任何方式对属性进行排序 显然 我们对上面的 1 没有任何疑问 因此我们可以合理地预期 对于任何操作 属性仅按照它
  • Laravel 5.5 由于存在不活动令牌,页面已过期

    我刚刚使用 php 7 2 在共享主机上部署了我的 laravel 应用程序 应用程序在 DigitalOcean 和我当地的 Homestead 上运行良好 但是当使用 php7 2 托管在共享主机上时 opt alt php72 usr
  • 将 youtube.com 中的视频嵌入到 iPhone 应用程序中

    我正在尝试将 youtube 视频嵌入到我的 iPhone 应用程序中 我正在使用 UIWebView 并将嵌入代码从 youtube 作为 html 字符串加载 所以我有一个带有基本 html 标记的布局 我将这段代码放在那里 问题是视频
  • 什么是 CSS 注入以及如何防止它?

    我听我的朋友谈论这个名为 CSS 注入 的漏洞 但是 我不知道这是什么 一听到它我就想 怎么可能使用 CSS 进行任何恶意活动或攻击 所以我想知道这个 CSS 注入 漏洞是什么以及如何预防它 它是什么 CSS 注入意味着攻击者设法将恶意 C
  • 在 Prolog 中查找图中两个节点之间的最短路径

    我想在 Prolog 中找到两个节点之间的最短路径 我想出了如何找到两个节点之间的所有路径 但不幸的是以下代码陷入了循环 arc a b arc b a arc b c arc c b arc c d arc d c path X Y ar
  • 是否可以在另一个语法定义中重复使用 boost::spirit::qi 语法?

    是否可以重复使用boost spirit qi另一种语法中的语法 例如作为规则 例如 如果我定义一个语法来将文本行解析为保存街道地址的结构 template lt typename iter gt struct address gramma
  • @SpringBootApplication - ComponentScan 在新的 Eclipse 项目中无法按预期工作

    我最近开始使用 Eclipse 从 IntelliJ Idea 迁移 但无论我开始编写新的 SpringBoot 应用程序 我总是遇到这个问题 我的主类上的 SpringBootApplication 未按预期工作 我应该能够在不添加 Co
  • CORS 请求在 docker-compose 环境中失败

    我希望弄清楚为什么在 Firefox 中运行的 React 应用程序中的 API 请求失败 标头如下 Accept Accept Encoding gzip deflate Accept Language en US en q 0 5 Co
  • 为什么 Kit Kat 需要使用 isValidFragment?

    自从 KitKat 发布以来 我注意到我的一大堆应用程序都更新了 修复 Kit Kat 中的崩溃 最近 当我发布自己的应用程序时 我发现其可能的来源是使用首选项活动的新 isValidFragment 要求 然而 我无法让任何人解释为什么突
  • 如何查询 postgres 的可选参数?

    我正在设置一个REST服务和我正在使用postgres作为数据存储 我想知道如何设置postgres查询以使用可选参数 IE SELECT from users where hair color 1 and eye color 2 其中 1
  • 支持 iOS 10 的 Xcode 7.3.1

    现在我有一个基于 Xcode 7 3 1 的应用程序 运行良好 但当我想将应用程序安装到 iOS 10 设备上时 出现了一个问题 提示 找不到开发人员磁盘映像 所以我找到了一个解决方案 如下 https danielemargutti co
  • React:为什么 `this.props.children` 未定义?

    我正在用 ReactJS 构建一个电子电阻计算器 我有一个组合组件声明如下 var ResistanceCalculator React createClass getInitialState function return bands 0
  • XSL if else 条件

    我有一个要求 我想要 if else 语句来检查节点是否具有属性或仅具有字符串 例如 1 个节点有0 File s found另一个具有诸如
  • 在 Kubernetes 中找不到持久卷声明

    目前我尝试实施持续量在我的 yaml 文件中 我在互联网上阅读了很多文档 但我不明白为什么当我进入仪表板窗格时会出现此消息 未找到持久卷声明 karaf conf pod yaml apiVersion v1 kind Pod metada
  • Linux 中 SVN 存储库目录放在哪里?

    我正在 Ubuntu Linux 上设置一个新的 SVN 服务器 放置存储库的好地方 最佳实践 在哪里 我应该创建一个新用户吗 服务器将通过 http 访问 因此无需创建用户帐户等 与 svn 的情况一样 提前谢谢了 我喜欢把东西放在下面
  • Python 访问 JSON 对象中的数据

    所以我在我的脚本中这样做 import json info json loads get info print info 哪个输出 richard richard desktop projects hello python python m