AVPlayer 不会播放来自 FFMPEG 的音频文件

AVPlayer won't play audio files from FFMPEG

在请求音频数据之前,AVPlayer 从 FFMPEG 请求字节范围 0-1。

FFMPEG 给出 200 响应,但 AVPlayer 需要 206 响应。

这导致请求失败,无法播放音频。

预期行为: 通过 ffmpeg

流式传输时播放曲目

当前行为:尝试使用 ffmpeg 进行流式传输时,我们得到“操作停止”

示例 FFMPEG 命令:

ffmpeg -i "/path/to/audio/track.mp3" -vn -strict -2 -acodec pcm_u8 -f wav -listen 1 -seekable 1 http://localhost:8090/restream.wav

玩家日志:

Error Domain=AVFoundationErrorDomain Code=-11850 "Operation Stopped" UserInfo={NSLocalizedFailureReason=The server is not correctly configured., NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x600003bcc4b0 {Error Domain=NSOSStatusErrorDomain Code=-12939 "(null)"}}
!av_interleaved_write_frame(): Broken pipe

!Connection to tcp://localhost:8090 failed: Connection refused

!Connection to tcp://localhost:8090 failed: Connection refused

!Connection to tcp://localhost:8090 failed: Connection refused

!Error writing trailer of http://localhost:8090/restream.wav: Broken pipe

此错误 defined by Apple 为:

+"The HTTP server sending the media resource is not configured as expected. This might mean that the server does not support byte range requests."

并在 this Whosebug post 中进行了很好的总结:

when AVPlayerItem receive a video URL , it do the following task:

    Send a bytes request HTTP Request, and range = 0 -1
    If the response code is 206 and return 1 bytes data, It do the 3th task, if not, AVErrorServerIncorrectlyConfigured error occurred.
    continue send other HTTP Request, to download segment of All duration. and the response of VideoData code must be 206

In my situation , when send range[0-1] HTTP request, the server side give me a 200 OK response, So error occurred.

网络日志:

GET /file.wav HTTP/1.1
Host: localhost:1234
X-Playback-Session-Id: F72F1139-6F4C-4A22-B334-407672045A86
Range: bytes=0-1
Accept: */*
User-Agent: AppleCoreMedia/1.0.0.18C61 (iPhone; U; CPU OS 14_3 like Mac OS X; en_us)
Accept-Language: en-us
Accept-Encoding: identity
Connection: keep-alive

HTTP/1.1 200 OK
Content-Type: application/octet-stream
Transfer-Encoding: chunked

转载using this sample app:

也可以使用标准 ffmpeg 并将 URL 添加到本地或远程 ffmpeg URL

进行复制

我们可以通过更改 FFMPEG 或 AVPlayer 来解决这个问题吗?

我在 ffmpeg 电子邮件列表上问过这个问题,但似乎不可能:

If I understand your request correctly, you are not asking for a change of a return type (you could probably do that yourself) but for the implementation of byte range requests. I don't think this is possible at all with FFmpeg.

也无法让 AVPlayer 忽略字节范围请求:

it is an apple standard that media providers need to support http 1.1 with the range header (check out the iTunes store guidelines for podcasts for example), so I wouldn't expect it anytime soon

SO Q'n: