V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yunfeihe
V2EX  ›  Python

如何用 Python 播放一个 MP4 格式的音频文件

  •  
  •   yunfeihe · 2018-01-29 21:14:01 +08:00 · 5843 次点击
    这是一个创建于 2250 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在用 python 写一个命令行下的豆瓣 FM,参考了命令行版的网易云音乐,同样准备用 linux 下的 mpg123 实现播放音乐功能,结果抓到豆瓣的音频数据是 mp4 格式的,不知如何是好。

    ps:搜索竟然发现在 V2EX 有人在前几年提过类似的问题,不过也是没解决。

    9 条回复    2018-01-30 13:46:27 +08:00
    loading
        1
    loading  
       2018-01-29 21:21:19 +08:00 via Android
    ffmpeg -i a.mp4 a.mp3
    Rheinmetal
        2
    Rheinmetal  
       2018-01-29 21:29:57 +08:00 via Android
    mediainfo 看看是什么编码 再重新封装 mp4 一般是 aac
    smwikipedia
        3
    smwikipedia  
       2018-01-29 21:47:10 +08:00 via Android
    除了封装,你能做什么呢?
    est
        4
    est  
       2018-01-29 22:48:16 +08:00
    windows 下之前是调用 windows media player 的 COM 接口
    linux 没记错的话,是 gstreamer
    mac 下可以用 2.X 自带的 Carbon 系列标准库去尝试调用 AVFoundation
    est
        5
    est  
       2018-01-29 23:04:27 +08:00
    mac 下亲测有效


    import time
    from Foundation import NSURL
    from AVFoundation import AVAudioPlayer

    f = NSURL.fileURLWithPath_('1.mp4')
    player = AVAudioPlayer.alloc().initWithContentsOfURL_error_(f, None)
    player.setNumberOfLoops_(0)
    player.play()
    time.sleep(3)
    player.stop()
    est
        6
    est  
       2018-01-29 23:05:22 +08:00
    记得用系统自带的 /usr/bin/python

    系统自带的才有 carbon / pyobjc 库。

    不要用 brew 或者 anaconda 安装的。
    MeteorCat
        7
    MeteorCat  
       2018-01-29 23:07:23 +08:00 via Android
    我记得以前用 lua,通过 c++编写 so 库接口给 lua 调用,我不清楚 python 加载机制是否和 lua 一样,可以尝试把这个功能转移给 c/c++
    yunfeihe
        8
    yunfeihe  
    OP
       2018-01-30 00:08:40 +08:00
    谢谢楼上几位,大概有思路了
    TimePPT
        9
    TimePPT  
       2018-01-30 13:46:27 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   948 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:33 · PVG 05:33 · LAX 14:33 · JFK 17:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.