使用 jQuery 确定视频文件大小

发布于 2024-11-15 04:20:29 字数 104 浏览 3 评论 0原文

我正在使用 videojs 播放不同尺寸的 html5 视频。是否可以使用 jQuery(或其他脚本语言)检测视频文件的高度/宽度,以便我可以将它们动态输入到嵌入代码中?

谢谢!

I am using videojs to play html5 videos, of varying dimensions. Is it possible to detect the height/width of a video file using jQuery (or another scripting language for that matter) so that I can dynamically input these into the embed code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南汐寒笙箫 2024-11-22 04:20:29

恐怕这不太可能。为了让浏览器能够确定视频的大小,它必须首先加载它。

一种解决方案可能是,假设这甚至可以工作,首先加载视频而不指定高度和宽度,而是使用 CSS 隐藏视频、计算尺寸、销毁视频并在单击时使用正确的尺寸再次加载它。这将是极其低效的。

或者,您可以制作一个服务器端脚本(PHP/ASP/...)来读取视频文件,检测内容最初保存时的高度和宽度,并将高度和宽度保存到数据库中。然后,您将能够在每个页面视图上从数据库读取高度和宽度。虽然您可以让这样的脚本在每次请求时读取视频文件,但这会效率低下,并且会增加生成页面所需的时间。

如果您使用 PHP,我强烈建议您查看 ffmpeg-php,这是一个免费且您可以使用开源库来解析多种类型的视频文件。

另一个想法:另一个解决方案可能是不在嵌入代码中指定高度和宽度,并让浏览器自行调整视频大小。然后,您可以调整灯箱的大小来修复视频容器。许多灯箱插件都有您可以挂钩的事件。

I'm afraid that's not quite possible. For the browser to be able to determine the size of the video, it has to load it first.

One solution could be, assuming that this would even work, to load the video first without specifying a height and width but using CSS to hide the video, calculate the dimensions, destroy the video and load it again on click using the right size. This would be incredibly inefficient.

Alternatively, you could make a server-side script (PHP/ASP/...) that reads the video file, detects the height and width when you content is originally saved and save the height and width to a database. You would then be able to read the height and width from the database on every page view. While you could make such a script read the video file on every request instead, this would be inefficient and would increase the time required to generate the page.

If you're using PHP, I'd highly recommend you look at ffmpeg-php, a free and open-source library you can use to parse several type of videos files.

Another thought: Another solution could be to not specify the height and width in the embed code and let the browser size the video on it's own. You could then resize the lightbox to fix the video container. Many lightbox plugins have events that you can hook into.

你是暖光i 2024-11-22 04:20:29

由于视频大小位于文件的元数据中,我认为您只能在服务器上访问它。流式传输的视频本身并不是完整的文件,并且不带有任何元数据。

因此,我建议您一个简单的解决方案(实际上是一种解决方法):在加载视频之前执行 $.ajax 请求。您的服务器将收到此请求并根据它可以访问的元数据返回大小。然后,在 ajax success 回调中,您将能够调整容器 height 并最终显示视频。

此外,还需要记住的是,每当有人观看视频时,访问文件来读取元数据并不是执行操作。因此,我们真的希望您能够正确配置 ajax 响应的缓存

Since the video size is in the file's metadata, I think you can only access it on the server. The streamed video isn't the full file itself and doens't arrive with any metadata.

So, I suggest you a simple solution (well, in fact a workaround): do an $.ajax request before loading the video. Your server will receive this request and returns the size based on the metadata that it can access. Then, on your ajax sucess callback you will be able to adjust the container height and finally shows the video.

Moreover, it's also important to remember that it isn't performative accessing the file to read metadata whenever someone is watching the video. So it's really wished that you correctly configure the caching of the ajax response.

你穿错了嫁妆 2024-11-22 04:20:29

是的,您可以获取 videoWidth 和 videoHeight 属性 - 它们是与视频的元数据一起下载的,因此请确保不要对视频元素使用 preload="none",因为它会阻止下载元数据,直到有人尝试播放视频。下面是使用 jquery 的方法:

$(function(){
  $('video').bind('loadedmetadata', function(){
    var rawWidth = $(this).prop('videoWidth');
    var rawHeight = $(this).prop('videoHeight');
  });
});

Yes, you can get the videoWidth and videoHeight properties - they are downloaded with the video's metadata, so be sure not to use preload="none" with your video element as it prevents metadata from being downloaded until someone tries to play the video. Here's how to do it with jquery:

$(function(){
  $('video').bind('loadedmetadata', function(){
    var rawWidth = $(this).prop('videoWidth');
    var rawHeight = $(this).prop('videoHeight');
  });
});
绾颜 2024-11-22 04:20:29

您可以使用 height 属性获取视频的高度(如果它在 元素上设置...

$('#idOfVideoElement').attr('height');

You could get the height of the video maybe using the height attribute (if it's set on the <video> element...

$('#idOfVideoElement').attr('height');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文