Django 从 Popen 进程向 StreamingHttpResponse 发送内容长度

标签 django audio ffmpeg streaming

我需要提供从大 wav 文件实时创建的 ogg 文件。
我可以发送内容,但我不知道为什么将数据的可变长度指示给 StreamingHttpResponse。

这是我的代码:

class OggAudioStreamer(object):

    def __init__(self, archivo):
        self.archivo = archivo

    def read(self, **kwargs):
        command = 'ffmpeg -i "{0}" -ac 1 -ar 22050 -acodec libvorbis -f ogg -'.format(self.archivo)
        args = split(command)
        response = Popen(args, stdout=PIPE, stderr=PIPE,
                         bufsize=8192, universal_newlines=False)
        response_iterator = iter(response.stdout.readline, b"")

        for resp in response_iterator:
            yield resp

def ogg_stream_response(request):
    data = OggAudioStreamer('SOME WAV FILE')
    stream = StreamingHttpResponse(data.read(), content_type='audio/ogg')
    return stream

最佳答案

如果我正确理解您的问题,您需要发送 Content-Length通过 StreamingHttpResponse 流式传输响应时的 header .如果是这种情况,那么这是不可能的,并且在 StreamingHttpResponse 中有明确说明。文档:

StreamingHttpResponse should only be used in situations where it is absolutely required that the whole content isn’t iterated before transferring the data to the client. Because the content can’t be accessed, many middlewares can’t function normally. For example the ETag and Content-Length headers can’t be generated for streaming responses.



在我看来,在请求处理期间对音频文件进行转码并不是一个好主意,因为您可以通过这种方式耗尽所有请求处理程序。相反,我会使用某种后台处理,例如 Celery .

关于Django 从 Popen 进程向 StreamingHttpResponse 发送内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32733641/

相关文章:

python - sse 连接过多导致网页挂起

python - 使用 settings.py 或 models.py 在 django 中自动创建数据库

objective-c - 为每个单元格行播放特定的音频文件

ffmpeg - 加快海报图像叠加

c# - 在 C# 中使用 AForge.Video.FFMEG 向图像添加文本

django - 有没有办法在 Django 模板中使 block 可选

touch-event - 动态壁纸onTouchEvent ACTION_DOWN仅播放声音片段的1/3

javascript - 从 socket.io 收到消息时从客户端播放音频 - node.js

android - 在 Android 上使用 FFMpeg 进行视频裁剪非常慢

python - mod_wsgi 解析 WSGI 脚本文件失败 |处理 WSGI 脚本时发生异常 |无法导入 'site' 模块