基于Wowza Apple HLS流,如何在播放器获取主m3u8时修复Chunklist URL
Hey there, I’ve helped several developers tackle exactly this problem with Wowza’s HLS streaming service—fixing those chunklist URLs in the master m3u8 every time a player requests it. Let’s walk through the most reliable solutions tailored to your scenario:
This is the go-to approach for most use cases because it lets you directly intercept and modify the m3u8 response at the Wowza level, even with dynamic logic.
Step 1: Write the Java Module
Create a simple module that hooks into Wowza’s HTTP request lifecycle to rewrite the chunklist URLs. Here’s a working example:
package com.wowza.wms.plugin.hlsfix; import com.wowza.wms.http.*; import com.wowza.wms.vhost.*; public class ModuleHLSChunkListFix extends ModuleBase { public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) { String requestURI = req.getRequestURI(); // Target only master m3u8 requests (adjust the path match to your app/stream) if (requestURI.contains("/sportplus4/_definst_/ngrp:plus4_all/") && requestURI.endsWith(".m3u8") && !requestURI.contains("chunklist_")) { try { // Grab the original m3u8 content String originalContent = new String(resp.getBody()); // Replace the placeholder URL with your actual public domain/port // Example 1: Replace with fixed public URL String fixedContent = originalContent.replaceAll("http://\\*:1935", "https://your-public-domain.com:1935"); // Example 2: Use relative paths instead (great if using a reverse proxy) // String fixedContent = originalContent.replaceAll("http://\\*:1935/sportplus4/_definst_/ngrp:plus4_all/", "./"); // Update the response with fixed content resp.setBody(fixedContent.getBytes()); resp.setContentLength(fixedContent.length()); } catch (Exception e) { getLogger().error("Failed to fix HLS chunklist URLs: " + e.getMessage()); } } } }
Step 2: Deploy the Module
- Compile the code into a JAR file and drop it into your Wowza installation’s
libfolder (e.g.,/usr/local/WowzaStreamingEngine/lib). - Log into the Wowza Manager, navigate to your
sportplus4application, go to the Modules tab, and add a new module with:- Name:
HLSChunkListFix - Description: Fixes chunklist URLs in master m3u8
- Class Name:
com.wowza.wms.plugin.hlsfix.ModuleHLSChunkListFix
- Name:
- Restart your Wowza service to apply the changes.
If your needs are simple (just static string replacement), you can use Wowza’s built-in rewrite functionality without writing any code.
- Open your Wowza
conf/VHost.xmlfile. - Find the
<HTTPProviders>section and add this rewrite provider:
<HTTPProvider> <BaseClass>com.wowza.wms.http.HTTPProviderRewrite</BaseClass> <RequestFilters>*.m3u8</RequestFilters> <AuthenticationMethod>none</AuthenticationMethod> <Properties> <Property> <Name>rewriteRules</Name> <Value> # Replace the placeholder URL in master m3u8 ^/sportplus4/_definst_/ngrp:plus4_all/(.*\.m3u8)$ replace:http://\*:1935/sportplus4/_definst_/ngrp:plus4_all/ with:https://your-public-domain.com:1935/sportplus4/_definst_/ngrp:plus4_all/ </Value> </Property> </Properties> </HTTPProvider>
- Save the file and restart Wowza. This will automatically rewrite all matching chunklist URLs in your master m3u8.
If you don’t want to modify Wowza’s configuration at all, you can use a reverse proxy like Nginx to intercept and rewrite the m3u8 content.
Here’s a sample Nginx config snippet:
server { listen 80; server_name your-public-domain.com; # Intercept master m3u8 requests and rewrite URLs location ~* /sportplus4/_definst_/ngrp:plus4_all/(?<filename>.*\.m3u8)$ { proxy_pass http://localhost:1935; sub_filter 'http://*:1935' 'https://your-public-domain.com:1935'; sub_filter_once off; # Replace all occurrences sub_filter_types application/vnd.apple.mpegurl; # Target HLS MIME type } # Pass through all other streaming requests location / { proxy_pass http://localhost:1935; } }
Quick Testing Tip
After setting up any of these solutions, verify the fix with curl:
curl http://your-wowza-ip:1935/sportplus4/_definst_/ngrp:plus4_all/playlist.m3u8
Check that the chunklist URLs now show your correct public domain instead of the http://*:1935 placeholder.
内容的提问来源于stack exchange,提问作者Rasoul Khalifeh

