VideoJS像YouTube一样调整视频大小

huangapple go评论84阅读模式
英文:

VideoJS scale video like youtube

问题

我正在尝试使用VideoJS来调整我的视频,以使其始终适应窗口的缩放,视频的尺寸是1280x720,我正在寻找一种解决方案,就像在YouTube剧院模式中一样,视频将居中并尽可能大,但会在两侧添加黑边。

当前代码:

  1. <html>
  2. <head>
  3. <title>Test Video</title>
  4. </head>
  5. <body>
  6. <link rel="stylesheet" href="video-js.css" type="text/css">
  7. <div class="container">
  8. <div class="VideoJS">
  9. <div id="Video">
  10. <video id="vid1" class="video-js vjs-fluid" controls preload="auto" playsinline webkit-playsinline
  11. style="max-width: 100%;">
  12. <source src="testvideo.mp4">
  13. </video>
  14. <script src="video.min.js"></script>
  15. <script>
  16. var player = videojs('vid1', {})
  17. </script>
  18. </div>
  19. </div>
  20. </div>
  21. </body>
  22. </html>

编辑:

以下代码将呈现出我想要的播放器行为,但它占据了整个页面,因此我需要它包含在一个div中,但仍然可以像这样缩放:

  1. <html>
  2. <head>
  3. <title>Test Video</title>
  4. </head>
  5. <body>
  6. <script src="video.min.js"></script>
  7. <link rel="stylesheet" href="video-js.css" type="text/css">
  8. <video id="my_video_1" style="width: 100%; height: 100%; top: 0px; position: absolute; background-color: black;"
  9. class="video-js" controls>
  10. <source src="http://vjs.zencdn.net/v/oceans.mp4" type='video/mp4'>
  11. </video>
  12. </body>
  13. </html>
英文:

I'm trying with VideoJS to scale my video so it always fits when window is scaling, the video is 1280x720 and looking for a solution like when you in youtbe theater mode, the video will be centered and as large as possbile but add black bars to side.

current code:

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;title&gt;Test Video&lt;/title&gt;
  4. &lt;body&gt;
  5. &lt;link rel=&quot;stylesheet&quot; href=&quot;video-js.css&quot; type=&quot;text/css&quot;&gt;
  6. &lt;div class=&quot;container&quot;&gt;
  7. &lt;div class=&quot;VideoJS&quot;&gt;
  8. &lt;div id=&quot;Video&quot;&gt;
  9. &lt;video id=&quot;vid1&quot; class=&quot;video-js vjs-fluid&quot; controls preload=&quot;auto&quot; playsinline webkit-playsinline
  10. style=&quot;max-width: 100%&quot;&gt;
  11. &lt;source src=&quot;testvideo.mp4&quot;&gt;
  12. &lt;/video&gt;
  13. &lt;script src=&quot;video.min.js&quot;&gt;&lt;/script&gt;
  14. &lt;script&gt;
  15. var player = videojs(&#39;vid1&#39;, {})
  16. &lt;/script&gt;
  17. &lt;/div&gt;
  18. &lt;/div&gt;
  19. &lt;/div&gt;
  20. &lt;/body&gt;
  21. &lt;/html&gt;

Edit:

This code below will present the player as I want it to behave but it takes up the whole page, so I need it to contain in a div but still scale as this code:

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;title&gt;Test Video&lt;/title&gt;
  4. &lt;body&gt;
  5. &lt;script src=&quot;video.min.js&quot;&gt;&lt;/script&gt;
  6. &lt;link rel=&quot;stylesheet&quot; href=&quot;video-js.css&quot; type=&quot;text/css&quot;&gt;
  7. &lt;video id=&quot;my_video_1&quot; style=&quot;width: 100%;height: 100%;top: 0px;position: absolute;background-color: black;&quot;
  8. class=&quot;video-js&quot; controls&gt;
  9. &lt;source src=&quot;http://vjs.zencdn.net/v/oceans.mp4&quot; type=&#39;video/mp4&#39;&gt;
  10. &lt;/video&gt;
  11. &lt;/body&gt;
  12. &lt;/html&gt;

答案1

得分: 0

以下是翻译好的内容:

"Ok I think I've solved it, this is the css

.container {
position: relative;
width: 100%;
height: 56.25vw;
max-height: calc(100vh - 250px);
min-height: 480px;
background: #000;
}

The max-height is subtraced the amount of space above the container div"

英文:

Ok I think I've solved it, this is the css

  1. .container {
  2. position: relative;
  3. width: 100%;
  4. height: 56.25vw;
  5. max-height: calc(100vh - 250px);
  6. min-height: 480px;
  7. background: #000;
  8. }

The max-height is subtraced the amount of space above the container div

huangapple
  • 本文由 发表于 2023年5月30日 05:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定