The Mysterious Case of the Video Tag: Why it Doesn’t Work in Chrome with HTTP Streams via VLC
Image by Edwards - hkhazo.biz.id

The Mysterious Case of the Video Tag: Why it Doesn’t Work in Chrome with HTTP Streams via VLC

Posted on

Have you ever tried to embed a video stream from VLC into your website using the HTML video tag, only to find that it refuses to work in Chrome? You’re not alone! Many developers have stumbled upon this issue, and today, we’re going to get to the bottom of it.

The Problem: Video Tag Won’t Play HTTP Stream from VLC in Chrome

The video tag is a powerful tool for embedding video content into web pages. It’s supported by all modern browsers, including Chrome. However, when you try to use it to stream video from VLC over HTTP, things start to get tricky. The video tag simply refuses to work, leaving you with a blank screen and a bunch of frustrated users.

What’s Going On? Is it Chrome, VLC, or the Video Tag?

Before we dive into the solution, let’s take a step back and understand what’s happening behind the scenes. When you use the video tag to stream video from VLC, the following sequence of events occurs:

  1. The video tag requests the video stream from VLC using the HTTP protocol.
  2. VLC responds with the video stream, but uses the RTSP (Real-Time Streaming Protocol) instead of HTTP.
  3. Chrome, being the strict browser that it is, refuses to play the video stream because it’s not served over a secure protocol (HTTPS).

So, it’s not entirely Chrome’s fault, nor is it VLC’s or the video tag’s. It’s a combination of factors that leads to this issue.

The Solution: Using a Secure Protocol and a Little Bit of Magic

Now that we know what’s causing the problem, let’s talk about how to fix it. To get the video tag working with HTTP streams from VLC in Chrome, you’ll need to:

  1. Use a secure protocol (HTTPS) to stream the video.
  2. Configure VLC to use the HTTP protocol instead of RTSP.
  3. Add a few magic words to your video tag.

Step 1: Configure VLC to Use HTTP

Open VLC and go to Media > Stream. In the Stream Output section, select the HTTP option and set the destination to a valid URL (e.g., http://localhost:8080). Make sure to choose a port that’s not already in use.

vlc -I http --http-host=localhost --http-port=8080 --http-password=password 
  --sout "#std{access=http,mux=ts,dst=:8080}" your_video_file.mp4

This will configure VLC to stream your video file over HTTP using the TS muxer.

Step 2: Add the Magic Words to Your Video Tag

Now, let’s create an HTML page with the video tag. Add the following code to your HTML file:

<video width="640" height="480" controls>
  <source src="http://localhost:8080" type="video/mp2t">
  Your browser does not support the video tag.
</video>

The magic words are type="video/mp2t", which tells the browser to expect a Transport Stream (TS) over HTTP. This is the key to getting the video tag working with VLC’s HTTP stream.

While the above solution works, it’s not secure. To add an extra layer of security, you can use a self-signed SSL certificate or obtain one from a trusted certificate authority. Then, update your VLC command to use HTTPS:

vlc -I http --https-host=localhost --https-port=8080 --https-password=password 
  --sout "#std{access=https,mux=ts,dst=:8080}" your_video_file.mp4

And update your video tag to point to the HTTPS URL:

<video width="640" height="480" controls>
  <source src="https://localhost:8080" type="video/mp2t">
  Your browser does not support the video tag.
</video>

Common Issues and Troubleshooting

While the above solution should work for most cases, you may encounter some issues. Here are some common problems and their solutions:

Issue Solution
VLC doesn’t start streaming Check that you’ve specified the correct port and that it’s not already in use.
The video tag doesn’t work in other browsers Try adding additional source tags with different mime types (e.g., video/mp4, video/webm) to support other browsers.
The video stream is buffering or stuttering Check your internet connection and adjust the VLC buffering settings. You can also try using a more efficient codec like H.264.

Conclusion

The mysterious case of the video tag not working in Chrome with HTTP streams from VLC has been solved! By configuring VLC to use HTTP, adding the magic words to your video tag, and optionally using a secure protocol, you can get your video stream working in Chrome and other browsers. Remember to troubleshoot common issues and adjust your setup accordingly.

Happy streaming!

Did you find this article helpful? Share your thoughts and experiences in the comments below!

Here are the 5 Questions and Answers about “video tag html does not work in Chrome when source is a http stream with vlc”:

Frequently Asked Question

Why doesn’t my video tag work in Chrome when the source is an HTTP stream from VLC?

Chrome has strict policies when it comes to media streaming, and by default, it doesn’t support HTTP streams from VLC. This is due to security restrictions and media format incompatibilities. Don’t worry, we can work around this!

What’s the main reason behind this video tag issue in Chrome?

The primary culprit is the lack of CORS (Cross-Origin Resource Sharing) headers in the HTTP stream from VLC. Chrome requires these headers to allow cross-origin requests, which are necessary for media streaming.

Can I fix this issue by adding some magic code to my HTML file?

Sorry, friend! Adding code to your HTML file won’t solve this issue. The problem lies with the VLC stream and Chrome’s security policies. You’ll need to configure VLC to include CORS headers or use a different streaming method that’s compatible with Chrome.

Is there a way to configure VLC to include CORS headers in the HTTP stream?

Yes, you can! You’ll need to add the following command-line option when starting VLC: `–http-header CORS header:Access-Control-Allow-Origin:*` This will include the necessary CORS headers in the HTTP stream, allowing Chrome to access the stream.

What’s the alternative to using VLC for streaming if I want to avoid these issues?

You can consider using alternative streaming methods, such as HLS (HTTP Live Streaming) or DASH (Dynamic Adaptive Streaming over HTTP), which are compatible with Chrome and other modern browsers. These formats are designed for adaptive bitrate streaming and offer better support for multi-platform playback.

Let me know if this meets your requirements!