IPython.display.Audio无法正确处理`.ogg`文件类型? [英] IPython.display.Audio cannot correctly handle `.ogg` file type?

查看:678
本文介绍了IPython.display.Audio无法正确处理`.ogg`文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Jupyter做一些音频分析,试图用 IPython.display.Audio .ogg 个文件>。由于PyCharm经常无法打开大的 .ipynb 文件,我大多使用Web浏览器在 localhost:8888 。





这张照片是我得到的与Chrome。如您所见, FailToDisplay.ogg 取自我的工作,音频播放栏未激活。 File-ACDC _-_ Back_In_Black-sample.ogg song sample.mp3 都是从Internet下载的。 3个文件的完整性都经过验证,即它们都可以通过音频播放器正确播放。



我还使用Microsoft Edge和Firefox进行了测试,结果是大多是一样的。 2 .ogg 播放栏都处于非活动状态,而 .mp3 播放栏处于活动状态并且运行正常。所以我猜这个问题不依赖于网络浏览器。



我用Chrome Developer Tool查看了这三个音频播放栏的html源代码,它们都像:

 < audio controls =controls> 
< source src =data:None; base64,VERYLONGTEXTtype =None>
您的浏览器不支持音频元素。
< / audio>

mp3的类型是<$ c ogg的$ c> audio / mpeg 类型。经过一些谷歌搜索,我想这与 MIME 类型有关。所以我用命令 mimetype 检查了3个音频文件:

  $ mimetype。 / * 
./AudioDisplayErrorTest.ipynb:text/ plain
./FailToDisplay.ogg:audio/x-vorbis + ogg
./File-ACDC_-_Back_In_Black-sample.ogg:video/ x-theora + ogg
./song sample.mp3:audio / mpeg

不是很奇怪。然后我发现这篇博客文章



它可能不是完美的解决问题,但至少它是有效的。所以现在就是这样。


I was doing some audio analysis stuff with Jupyter and tried to play .ogg files with IPython.display.Audio. Since PyCharm often failed to open big .ipynb files, I mostly used web browser to view my Notebook files at localhost:8888.

This picture is what I get with Chrome. As you can see, FailToDisplay.ogg is taken from my work, the audio play bar is not activated. File-ACDC_-_Back_In_Black-sample.ogg and song sample.mp3 are all downloaded from Internet. The integrity of 3 files are all validated, i.e., they can all be played correctly with audio players.

I also tested it with Microsoft Edge and Firefox, and the results are mostly the same. 2 .ogg playbars are all inactive while .mp3 playbar is active and works perfectly. So I guess the problem is not web browser dependent.

I checked the html source code of these 3 audio playbars with Chrome Developer Tool, they are all like:

<audio controls="controls">
                    <source src="data:None;base64,VERYLONGTEXT" type="None">
                    Your browser does not support the audio element.
                </audio>

The type for mp3 is audio/mpeg and type for ogg is None. After some google search, I guess this has something to do with MIME type. So I inspected 3 audio files with command mimetype:

$ mimetype ./*
./AudioDisplayErrorTest.ipynb:          text/plain
./FailToDisplay.ogg:                    audio/x-vorbis+ogg
./File-ACDC_-_Back_In_Black-sample.ogg: video/x-theora+ogg
./song sample.mp3:                      audio/mpeg

Not very strange. Then I find this blog post How to set MIMETYPES on server : Forums : PythonAnywhere and tested my python MIME type settings:

>>> import mimetypes
>>> mimetypes.guess_type("foo.ogg")
(None, None)

Now I don't know what to do next with this kind of situation. Is this just a bug of Jupyter or IPython or system-wide? Where can I change this behavior?

My Python environment settings are

audioread==2.1.4
ipykernel==4.4.1
ipython==5.1.0
ipython-genutils==0.1.0
ipywidgets==4.1.1
jupyter==1.0.0
jupyter-client==4.3.0
jupyter-console==5.0.0
jupyter-core==4.1.1
librosa==0.4.3
nbconvert==4.2.0
nbformat==4.0.1
notebook==4.2.2
numpy==1.11.1
openpyxl==2.3.2
pydub==0.16.5

解决方案

Since no one gives a hint, I guess I'll have to work alone...

First look into the source code of IPython.display.audio: ipython/display.py at 48b01aadcbb6a53d2c77fa250c8a4344931c357c · ipython/ipython

def _repr_html_(self):
    src = """
            <audio controls="controls" {autoplay}>
                <source src="{src}" type="{type}" />
                Your browser does not support the audio element.
            </audio>
          """
    return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr())

This is the code that generates html source code of audio control block, type is assigned from self.mimetype. And self.mimetype is derived from reload():

    if self.filename is not None:
        self.mimetype = mimetypes.guess_type(self.filename)[0]
    elif self.url is not None:
        self.mimetype = mimetypes.guess_type(self.url)[0]
    else:
        self.mimetype = "audio/wav"

It's obvious if mimetypes.guess_type("filename.ogg")[0] gets None, then we have type == None, which results an inactive audio control block.

From 18.7. mimetypes — Map filenames to MIME types — Python 2.7.12 documentation I learned that MIME types can be loaded from file or dynamically added with mimetypes.add_type(). It also said by default mimetypes will load from Windows registry. I tried to modify system-wide MIME type settings of .ogg with one small utility FileTypesMan - Alternative to 'File Types' manager of Windows but it did not reflect on mimetypes, so I guess I'll have to let it go.

At last I realized that a monkey patch before IPython.display.Audio was used will possibly work and it really does:

It may not be perfect to solve the problem, but at least it works. So be it for now.

这篇关于IPython.display.Audio无法正确处理`.ogg`文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆