【问题标题】:Python Twisted web server audio filePython Twisted Web 服务器音频文件
【发布时间】:2023-03-03 23:36:01
【问题描述】:

我正在尝试用 python 扭曲创建一个简单的 Web 服务器。不过,我在提供 m4a 音频文件时遇到了麻烦。

在当前程序中,当我加载http://localhost:8880/mp3.html 时,它工作正常。它显示音频播放器和 mp3 播放。此外,程序同时打印“/mp3.html”和“/test.mp3”。

但是,当我加载 http://localhost:8880/m4a.html 时,它不起作用。它显示音频播放器,但 m4a 不播放。另外,程序只打印“/m4a.html”,不打印“/test.m4a”。

我当前的代码如下。

import urlparse
import os
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web.static import File
import time
import subprocess
import mimetypes

class playM4A(Resource):
    isLeaf = True
    def render_GET(self, request):
        this=urlparse.urlparse(request.path)#scheme,netloc,path,query
        root,ext=os.path.splitext(this.path)
        filename=os.path.basename(request.path)
        fileFolder=request.path.replace(filename,"")
        self.serverRoot=os.getcwd()
        print request.path
        if ext==".m4a":
            thisFile=File(self.serverRoot+request.path)
            return File.render_GET(thisFile,request)
        elif ext==".mp3":
            thisFile=File(self.serverRoot+request.path)
            return File.render_GET(thisFile,request)
        elif filename=="m4a.html":
            return """
<html>
<audio controls>
  <source src="http://localhost:8880/test.m4a" type="audio/mp4a-latm">
Your browser does not support the audio element.
</audio>
not m4a </html>"""
        elif filename=="mp3.html":
            return """
<html>
<audio controls>
  <source src="http://localhost:8880/test.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
not m4a </html>"""

resource = playM4A()
factory = Site(resource)
reactor.listenTCP(8880, factory)
reactor.run()

【问题讨论】:

  • 它似乎适用于mp3,所以我认为它可能与m4a文件格式有关。
  • 我怀疑Content-Type: 标头不正确。您能否使用 wget、curl、Python 请求或任何其他方便的工具来确定 M4A GET 和 MP3 GET 返回的内容类型?
  • 另外你使用的是什么操作系统?什么浏览器?
  • 我正在使用带有 chrome 的 mac。让我看看能不能看到内容类型
  • 另外,一件奇怪的事情是,它没有执行“打印请求路径”,这可能表明它甚至没有请求 m4a。

标签: python twisted m4a twisted.web


【解决方案1】:

如果您将 audio/mp4a-latm 更改为 audio/mp4,则该代码有效

【讨论】:

    最近更新 更多