基于ffmpeg、hls-server实现HLS流转发服务的故障排查请求
Let's break down your issues step by step and fix them one by one:
1. Fix File Generation & HLS Server Path Mapping
Your core problem is a mismatch between where FFmpeg saves files and where your HLS Server looks for them. Here's what to adjust:
First, Correct HLS Server Configuration
The hls-server library requires both a HTTP path (what users request in the browser) and a local directory (where your HLS files are stored). You only set the HTTP path before—add the dir parameter to link it to your local storage:
var ffmpeg = require('../index'); var HLSServer = require('hls-server') var http = require('http') var fs = require('fs') // Ensure the local streams directory exists (create it if not) const streamsDir = './streams'; if (!fs.existsSync(streamsDir)) { fs.mkdirSync(streamsDir, { recursive: true }); } var server = http.createServer() // Map browser requests to http://localhost:4000/streams/ to your local ./streams folder var hls = new HLSServer(server, { path: '/streams', dir: streamsDir }) server.listen(4000, () => { console.log('HLSServer ready on http://localhost:4000') })
Then, Fix FFmpeg Output Paths
Update your FFmpeg command to save files directly to the mapped local directory, and set the correct hls_base_url so the m3u8 playlist points to valid TS file URLs:
const command = ffmpeg('https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8') .outputOptions([ '-hls_time 10', '-hls_playlist_type event', // Save TS files to the local streams directory `-hls_segment_filename ${streamsDir}/%03d.ts`, // Point to the HTTP path your HLS Server uses '-hls_base_url http://localhost:4000/streams/' ]) // Save the m3u8 playlist to the local streams directory .output(`${streamsDir}/outputfile.m3u8`) .on('progress', function (progress) { console.log('Processing: ' + (progress.percent || 0) + '% done') }) .on('end', function () { console.log('Finished processing!') }) .on('error', function(err) { console.error('FFmpeg error:', err) }) .run()
After this change, you'll be able to access your stream at http://localhost:4000/streams/outputfile.m3u8.
2. Fix VLC Playback Issues
When you removed -hls_base_url, FFmpeg generated relative paths for TS files in the m3u8. This works if you open the local m3u8 file directly in VLC, but fails over HTTP because the browser/VLC can't resolve the relative paths to your server's URL. The corrected hls_base_url above fixes this by using full HTTP URLs for TS files.
3. Fix "URL read error: -10053" in CMD Command
You can't output FFmpeg streams directly to an HTTP URL like http://localhost:4000/outputfile.m3u8—your HLS Server is a static file server and doesn't support receiving HTTP write requests from FFmpeg. Stick to outputting to local files first, then let the HLS Server serve them.
A corrected CMD command would look like this (make sure the streams directory exists first):
.\ffmpeg.exe -i "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8" -hls_time 10 -hls_playlist_type event -hls_segment_filename streams/%03d.ts -hls_base_url http://localhost:4000/streams/ streams/outputfile.m3u8 -loglevel verbose
Quick Additional Checks
- Ensure your Node.js process has permission to create/write to the
./streamsdirectory - Wait for FFmpeg to generate at least one TS file before testing playback (incomplete playlists will fail)
- Double-check that FFmpeg is installed correctly and accessible in your PATH (or use the full path to
ffmpeg.exein your code/CMD)
内容的提问来源于stack exchange,提问作者kieon.giuseppe

