简体   繁体   English

如何使用 webpack 在 React 中加载本地视频?

[英]How to load a local video in React using webpack?

I can't seem to figure out how to get an html5 video to render in a react app from local files.我似乎无法弄清楚如何从本地文件中获取 html5 视频以在 React 应用程序中呈现。 Literally the only way I've been able to get this to work is like this:从字面上看,我能够让它发挥作用的唯一方法是这样的:

<video src="http://www.w3schools.com/html/movie.mp4" controls />

Here's what I've tried这是我尝试过的

1. Including the path directly 1.直接包含路径

<video src={require('path/to/file.mp4')} controls />

which returns an error返回错误

Module parse failed: /path/to/file.mp4 Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.

2. Adding these loaders one at a time to the webpack config 2.将这些加载器一次一个添加到 webpack 配置中

{
  test: /\.(mp4)$/,
  loader: 'file'
  // loader: 'url-loader'
  // loader: 'url-loader?limit=100000'
  // loader: 'file-loader'
  // loader: 'file-loader?name=videos/[name].[ext]'
},

this spit out the following error in the browser这在浏览器中吐出以下错误

GET http://localhost:3000/530c2bf99dad0f857d46940b62b84946.mp4 404 (Not Found)

3. I tried adding a direct url to the file 3.我尝试在文件中添加一个直接 url

<video src={require('http://localhost:3000/path/to/file.mp4')} controls />

but still errors:但仍然错误:

Module not found: Error: Cannot resolve module 'http://localhost:3000/path/to/file.mp4' in path/to/file.mp4

4. I tried adding the mp4 extension in my webpack config like this person did 4.我试着像这个人那样在我的 webpack 配置中添加 mp4 扩展

{
  test: /\.jpe?g$|\.gif$|\.png$|\.ico|\.svg$|\.woff$|\.ttf$|.mp4$/,
  loader: 'url-loader?limit=8192'
}

but no luck但没有运气


5. I started implementing webpack-isomorphic-tools but then I wasn't sure if this was the right direction so I paused on it. 5.我开始实现webpack-isomorphic-tools,但后来我不确定这是否是正确的方向,所以我暂停了它。 It seems like this guy got it working this way.看来这家伙是通过这种方式工作的。 ( see file ) 见文件


I also noticed in the webpack documentation under loader conventions that file-loader will load video files.我还在加载器约定下的 webpack 文档中注意到file-loader将加载视频文件。 webpack 文档图像 Does this mean webpack won't load other video types such as .mov, .vob, .avi, etc. ?这是否意味着 webpack 不会加载其他视频类型,例如.mov, .vob, .avi, etc.

If you want to take a look at the code, here's the repository .如果你想看一下代码,这里是存储库


Resources资源

I had the same problem, my solution is:我遇到了同样的问题,我的解决方案是:

It uses https://github.com/webpack/html-loader and https://github.com/webpack/url-loader :它使用https://github.com/webpack/html-loaderhttps://github.com/webpack/url-loader

The loaders configuration:加载器配置:

  loaders: [{
      test: /\.html$/,
      loader: 'html-loader?attrs[]=video:src'
    }, {
      test: /\.mp4$/,
      loader: 'url?limit=10000&mimetype=video/mp4'
  }]

And in html:在 html 中:

A simple video tag, remeber webpack uses path referenced to the html file.一个简单的视频标签,记住 webpack 使用引用到 html 文件的路径。

<video src="../../../assets/videos/cover1.mp4"></video>

This works for me.这对我有用。

References: https://github.com/webpack/html-loader/issues/28参考资料: https : //github.com/webpack/html-loader/issues/28

Although this is an old post, and the question is specifically about WebPack...虽然这是一篇旧帖子,但问题是专门关于 WebPack ...

If you're using Typescript and a boilerplate such as create-react-app , and if you have your video on the static folder, then please check out this solution, of adding the type to src/react-app-env.d.ts , before ejecting your app:如果您使用的是Typescript和样板文件,例如create-react-app ,并且您的视频在静态文件夹中,那么请查看此解决方案,将类型添加到src/react-app-env.d.ts ,在弹出您的应用程序之前:

https://github.com/facebook/create-react-app/issues/6822 https://github.com/facebook/create-react-app/issues/6822

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM