让Safari在HTML 5视频标签中更喜欢HEVC

Cha*_*ark 6 video mp4 cross-browser hevc

I'm currently investigating the feasibility of adding HEVC support to videos but am hitting a problem with Safari. Here's the sample source:

<video autobuffer="true" autoloop="" loop="" controls="">
    <source src="film_WebM.webm" type="video/webm">
    <source src="film_HEVC.mp4" type="video/mp4">
    <source src="film.mp4" type="video/mp4">
</video>
Run Code Online (Sandbox Code Playgroud)

Ideally a browser should read the sources and takt the first file that it thinks it can read and this should allow Firefox and Chromium to show the VP9 film, Safari the HEVC and Internet Explorer the H264. However, Safari doesn't play nicely and will ignore the HEVC film if the H264 is present. I've tried annotating the source with codec information but this doesn't help. Setting HEVC as the default source for the video element works for Safari but causes problems for every other browser.

Is there any way to solve this without using feature detection to manipulate the src element?

Filed with Apple as a Safari bug #37821806

The discussion below suggests that Safari could, despite Apple's own release notes, be deciding to use an AVC source based on hardware considerations. How it manages to do this without codec hint or apparent mime-type sniffing is unclear. It would be useful if people could test the codepen demo and note in comments what codec play and Mac hardware info.

Nic*_*ick 9

首先确保您的 HEVC 视频编码正确,以便 Safari 可以播放。通过从视频标签中删除所有源(指向您的 HEVC 视频的源除外)来测试这一点。如果 Safari 播放它,请继续下一步,否则修复视频文件。

一旦您确定 HEVC 视频与 Safari 兼容,请使用标签中的type属性向浏览器提供有关编解码器的提示source。到目前为止,您只是告诉浏览器 MP4 容器用于 HEVC 和 H.264 源。浏览器必须下载部分文件才能确定哪些文件是兼容的,通常是按照source标签的顺序。

您可以/应该指定有关使用的所有编解码器的详细信息,包括视频编解码器,如果涉及音频,还应指定音频编解码器。

  • avc1 代表 H.264
  • hvc1 代表 H.265 (HEVC)

对于您的示例,最短的版本是这样的:

<video>
    <source src="film_WebM.webm" type="video/webm">
    <source src="film_HEVC.mp4" type='video/mp4; codecs="hvc1"'>
    <source src="film.mp4" type='video/mp4; codecs="avc1"'>
</video>
Run Code Online (Sandbox Code Playgroud)

另请参阅:CodePen 上的演示文章

更新

感谢您在评论中的反馈,我想我知道发生了什么:Safari 似乎会考虑您的硬件来选择最佳视频源,这实际上非常聪明。H.264 的硬件支持广泛可用,即使在较旧的硬件上也是如此,而 HEVC/H.265 硬件支持则不然,并且需要更多的 CPU,最终也需要更多的能源(电池)。

因此,尽管您的设备(例如 MBP)和软件可以解码和播放 HEVC 视频,但 Safari 可能更喜欢 H.264 视频源(如果可用)。

我做了更多的测试:

  • iMac 2014 年末 (5K):?HEVC
  • iPhone X(iOS 12 测试版):?HEVC
  • iPhone 7 Plus(iOS 11):?HEVC
  • 较旧的 iPad Air(iOS 11):?? 更喜欢 H.264,但如果没有 H.264 源可用,将播放 HEVC 视频