Webpack 3 locates .mp4 file but video is not playable

主宰稳场 提交于 2019-12-01 01:57:15

Specify output file name in webpack.config.js

  1. View the Wepback Documentation: file-loader

  2. Here is what your loader should look like:

    {
      test: /\.(mov|mp4)$/,
      use: [
        {
          loader: 'file-loader',
          options: {
            name: '[name].[ext]'
          }  
        }
      ]
    }
    

    Important!

    You must also import your videos into your main.js file like so:

    main.js

    import video_2 from '../media/video_1.mp4';
    import video_1 from '../media/video_2.mov';

  3. Now, in index.html (inside your src/ directory), you can set the src attribute of your video tag to a relative path that would point to your video when it loads into the dist/ directory.
    Your path should look something like this:

        <video loop autoplay controls>
          <source src="./video_1.mp4" type="video/mp4">
        </video>
    
  4. Now run npm run build or npm run build:dev

You can optionally specify a path like so:

webpack.config.js

    {
      test: /\.(mov|mp4)$/,
      use: [
        {
          loader: 'file-loader',
          options: {
            name: '[path][name].[ext]'
          }  
        }
      ]
    }

Update

You can also use a CDN to deliver your video. Simply set the src of your video element to a URL.

ie.

src="https://cloudflare.com/?user=574983038&video=cat.mp4"

If you don't want to pay for a CDN, you can always just use a remote server. I realize that also costs money, but chances are, you probably already have one. Just make sure you have CORS set to deliver to your website:

Example using PHP

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");

Since NodeJS has become very popular, this is probably the best method of delivery anyways. As of today (5/11/19), if you try to load your video from a relative path inside your component from a NodeJS server whilst making a GET request to any path other the root directory, it won't be able to find your file. For example, if you have a video located at https://example.com/videos/593020, and someone makes a GET request to that URL, your video won't load because NodeJS won't know where to find it.

I could be wrong. You might be able to load it by importing it somewhere else. What I do know is that Node attempts to get your video from whatever path you specify using the path of window.location.href. Using a CDN is much easier.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!