我想知道是否有任何直接的方法可以实现这种效果,而不需要后端代码来提取帧,将其保存为 jpg 并将其数据库存储在某个地方。
当视频加载时,视频的第一帧只显示为海报的效果会非常有用(只有当浏览器可以明显地播放视频时它才会起作用,这可能与海报的方式有点不同)传统上是可行的,但这不是一个问题。
I'm wondering if there's any straightforward way to achieve this effect, without needing backend code to extract a frame, save it as a jpg and database it somewhere.
An effect whereby the first frame of the video just shows up as the poster when the video loads would be so helpful (it would only work if the browser can play back the video obviously, which might be a little different from how poster
traditionally works, but that is not a concern.
发布评论
评论(9)
您尝试过以下操作吗?
只需将时间(以秒为单位)#t={seconds} 附加到源 URL:
我选择了秒的一小部分(0.1)来保持较小的帧数,因为我怀疑如果你放置1秒,它会“预加载”第一个1 秒的视频(即 24 帧或更多......)。以防万一...
在桌面版 Chrome 和 Firefox 上运行良好:)
不过,不适用于 Android 手机:(
我还没有在 iOS、iPhone、IE 上测试过??
2021 年 5 月编辑:
我意识到许多现代浏览器现在会自动显示第一帧的海报。
看起来他们听到了我们的声音:-)
Did you try the following?
just append time in seconds #t={seconds} to source URL:
I have chosen a fraction of second (0.1) to keep number of frames small, because I have the suspect that if you put 1 second, it would "preload" the first 1 second of video (i.e. 24 frames or more ....). Just in case ...
Works fine on Chrome and Firefox on desktop :)
Works not on Android mobile, though :(
I did not test on iOS, iPhone, IE yet ??
Edit May 2021:
I realized that many modern browsers now show automatically a poster of first frame.
Seems like they heard us :-)
为了简单起见,您只需将
preload="metadata"
添加到您的视频标记,并将第一帧的第二个#t=0.5
添加到您的视频来源:祝你好运!
To make it simple you can just add
preload="metadata"
to your video tag and the second of the first frame#t=0.5
to your video source:Best of luck!
有一个名为 Popcorn.js 插件rel="noreferrer">Popcorn.capture 允许您从 HTML5 视频的任何帧创建海报。
浏览器施加了一个限制,禁止读取跨域请求的资源的像素数据(使用画布 API 记录帧的当前值)。源视频必须托管在与请求源视频的脚本和 html 页面相同的域中,才能使此方法发挥作用。
使用此插件创建海报的代码非常简单:
There is a Popcorn.js plugin called Popcorn.capture which will allow you to create posters from any frame of your HTML5 video.
There is a limitation that is imposed by the browser that prohibits reading pixel data of resources requested across domains (using the canvas API to record the current value of a frame). The source video must be hosted on the same domain as the script and html page that is requesting it for this approach to work.
The code to create poster using this plugin is quite simple:
我最近为一个可在桌面和移动设备上运行的项目执行了此操作。诀窍是让它在 iPhone 上运行。
设置
preload=metadata
适用于桌面和 Android 设备,但不适用于 iPhone。对于 iPhone,我必须将其设置为
自动播放
,以便海报图像在初始加载时自动显示。 iPhone 会阻止视频自动播放,但结果会显示海报图像。我必须使用此处找到的 Pavan 答案对 iPhone 进行检查。 检测 iPhone 浏览器。然后根据设备使用带或不带
自动播放
的正确视频标签。I recently did this for a recent project that works on desktop and mobile. The trick was getting it to work on iPhone.
Setting
preload=metadata
works on desktop and android devices but not iPhone.For iPhones I had to set it to
autoplay
so the poster image automatically appears on initial load. iPhones will prevent the video from auto playing, but the poster image appears as the result.I had to do a check for iPhone using Pavan's answer found here. Detect iPhone Browser. Then use the proper video tag with or without
autoplay
depending on the device.您可以在视频元素上设置
preload='auto'
以自动加载视频的第一帧。You can set
preload='auto'
on the video element to load the first frame of the video automatically.#2、#3 等帧的解决方案。我们需要附加一次性处理程序 .one() 来重置默认框架。
Solution for #2, #3 etc. frames. We need attach disposable handler .one() for resetting default frame.
我找到了一种将海报动态添加到视频的好方法!
要显示视频中所需的帧(在我的例子中是 1.75 秒的帧) - 将
preload="metadata"
添加到视频元素,并将#t=1.75
添加到源 URL 末尾。将 eventListener 添加到视频元素,该元素仅侦听一次
play
事件。事件发出后重置视频时间。
I found a great way to dynamically add poster to a video!
To show the desired frame from video (in my case it's the frame at 1.75 seconds) - add
preload="metadata"
to the video element and#t=1.75
to the end of source URL.Add eventListener to the video element that will listen for
play
event only once.Once the event is emitted reset the video time.
对于其他想要将图像指定为海报的人,请使用
海报属性
:For others, who wants to specify image as poster, use
poster attribute
:虽然现在第一帧通常会自动加载(至少在使用
preload="metadata"
时),但这似乎在某些移动场景中不起作用,例如在带有 Cordova 的 Android WebView 中。 (另请参阅此问题。)我对此特别的解决方案是添加一个
autoplay
属性和play
上执行.pause()
的一次性事件侦听器:While nowadays the first frame is usually loaded automatically anyway (at least when using
preload="metadata"
), this doesn't seem to work in some mobile scenarios, such as within a WebView an Android with Cordova. (See also this issue.)My solution for this special was to add an
autoplay
attribute and a one-time event listener onplay
that does.pause()
: