计算文件大小的响应? [英] calculating filesize of a response?

查看:163
本文介绍了计算文件大小的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个脚本来执行视频和音频的动态转码(可能只是现在的音频)和流式传输到移动设备,目前我使用这里发布在其他地方的脚本(如何将音频文件(即时转换)转换为浏览器? )但是这似乎有问题,因为我不知道如何计算输出数据的文件大小,任何人都可以建议我如何动态计算并传递正确的标题?

im tryingto code a script to do dynamic transcoding of video and audio (likely just audio for now) and streaming to mobile devices, currently im using the script posted elsewhere on here (How do you convert audio files (on the fly) to the browser?) but this seemsto have problems as i do not know how to calculate the filesize of the outputted data, can anyone suggest how i might calculate this dynamicly and pass the correct header?

推荐答案

(最后解决具体问题的底部的答案。)

在这种情况下(和其他大多数),音频和视频编码在确切的文件大小上是不可预测的。记住,您的源材料的特性和对最终文件大小的影响在编码时无法预测。如果你正在转载,你正在做两件事情:1)压缩,2)丢失数据。它不能用mp3输出格式。

Audio and video encoding is, in this case (and most others), not predictable when it comes to the exact resulting file size. Remember the fact that your source material's characteristic and impact on the final file size cannot be predicted while encoding either. If you are transcoding 'on the fly', you are doing two things 1) compressing, 2) losing data. It just cannot be done with an mp3 output format.

你可能...如果 :编码是无损,并且您的算法被调整到非常具体的源材料特性(采样率,样本大小等)以及未压缩的源和目标格式。但这不是你的情况。

You might...if: the encoding was lossless, and your algorithm was tuned to very specific source material characteristics (sample rate, sample size, etc.) and both the source and target formats where uncompressed. But that is not your situation.

至于标题:不要发送! Content-Length 头不是HTTP 1.1要求。这有缺点(进度条不能知道什么100%完成,直到文件结尾;没有剩余时间也可能),但我的猜测是,你可以没有它生活。

As for the header: don't sent it! The Content-Length header is not an HTTP 1.1 requirement. There are disadvantages to this (progress bars cannot ever know what 100% done until end-of-file; no 'time remaining' is possible either) but my guess is you can live without it.

基于评论的最终讨论:

Final discussion based on comments:

使用浏览器,我得到您正在描述的行为。随着这个curl命令(有助于调试这样的低级痛苦),它也不起作用:

With a browser, I get the behavior you are describing. And with with this curl command (helpful for debugging low level pain like this), it also does not work:

curl --trace-ascii trace0.txt "http://dmpwap.net/playmp3.php?b=128&file=Red_Hot_Chili_Peppers_-_15_-_Fortune_Faded.mp3" > test0.mp3

我得到0个字节,在我的跟踪中看到:

I get 0 bytes, and see this in my trace:

manoa:~ stu$ cat trace0.txt == Info: About to connect() to dmpwap.net port 80 (#0)
== Info:   Trying 64.191.50.69... == Info: connected
== Info: Connected to dmpwap.net (64.191.50.69) port 80 (#0)
=> Send header, 213 bytes (0xd5)
0000: GET /playmp3.php?b=128&file=Red_Hot_Chili_Peppers_-_15_-_Fortune
0040: _Faded.mp2 HTTP/1.1
0055: User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.1
0095: 9.4 OpenSSL/0.9.8k zlib/1.2.3
00b4: Host: dmpwap.net
00c6: Accept: */*
00d3: 
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 19 bytes (0x13)
0000: Connection: close
<= Recv header, 37 bytes (0x25)
0000: Date: Fri, 11 Dec 2009 14:04:58 GMT
<= Recv header, 27 bytes (0x1b)
0000: Server: Microsoft-IIS/6.0
<= Recv header, 27 bytes (0x1b)
0000: X-Powered-By: PHP/5.2.9-2
<= Recv header, 35 bytes (0x23)
0000: Content-Transfer-Encoding: binary
<= Recv header, 26 bytes (0x1a)
0000: Content-Type: audio/mpeg
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 0 bytes (0x0)
== Info: Closing connection #0



但是....



如果我添加了一个 - tcp-nodelay 选项,它工作很好!例如:

curl --tcp-nodelay --trace-ascii trace1.txt "http://dmpwap.net/playmp3.php?b=128&file=Red_Hot_Chili_Peppers_-_15_-_Fortune_Faded.mp3" > test1.mp3

它返回3219104字节。 trace.txt如下所示:

It returned 3219104 bytes. The trace.txt looks like this:

== Info: About to connect() to dmpwap.net port 80 (#0)
== Info:   Trying 64.191.50.69... == Info: TCP_NODELAY set
== Info: connected
== Info: Connected to dmpwap.net (64.191.50.69) port 80 (#0)
=> Send header, 213 bytes (0xd5)
0000: GET /playmp3.php?b=128&file=Red_Hot_Chili_Peppers_-_15_-_Fortune
0040: _Faded.mp3 HTTP/1.1
0055: User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.1
0095: 9.4 OpenSSL/0.9.8k zlib/1.2.3
00b4: Host: dmpwap.net
00c6: Accept: */*
00d3: 
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 19 bytes (0x13)
0000: Connection: close
<= Recv header, 37 bytes (0x25)
0000: Date: Fri, 11 Dec 2009 13:56:47 GMT
<= Recv header, 27 bytes (0x1b)
0000: Server: Microsoft-IIS/6.0
<= Recv header, 27 bytes (0x1b)
0000: X-Powered-By: PHP/5.2.9-2
<= Recv header, 35 bytes (0x23)
0000: Content-Transfer-Encoding: binary
<= Recv header, 26 bytes (0x1a)
0000: Content-Type: audio/mpeg
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 1258 bytes (0x4ea)
0000: ID3.......TENC.......Lavf52.23.1...d.... ..=....w......oq......0
   ...  {many lines}
0180: UUUUUU
== Info: Closing connection #0

我可以听歌(3m21s,立体声,mpga,48kHz,128kbps)没有问题。

I can listen to the song (3m21s, stereo, mpga, 48kHz, 128kbps) with no problems.

所以,我的理论是,因为流中有连续的0x00字节,客户端正在考虑好的,我有0x00,0x00 ...并没有发送任何其他信息,连接必须结束。但是,在卷曲客户端上设置的 - tcp-nodelay 选项不会发生

So, my theory is, that because there are consecutive 0x00 bytes in the stream, the clients are thinking "OK, I got 0x00, 0x00...and nothing else has been sent, the connection must be over." But with the --tcp-nodelay option set on the curl client, it does not happen.

我的候选解决方案:禁用 Nagel的算法(在套接字选项上设置TCP无延迟)服务器端,至少对于这些转码请求连接。这将防止缓冲,我怀疑导致连接被丢弃。

My candidate solution: Disable Nagel's algorithm (set TCP no delay on socket options) server side, at least for these transcoding request connections. This will prevent buffering, which I suspect is leading to the connections being dropped.

这篇关于计算文件大小的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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