FFmpeg新手求助:如何按递增序列输入批量.ts分片文件?
Hey there! As someone new to FFmpeg and streaming tech, let's break this down clearly—covering the method you're thinking of, plus a far more efficient approach that'll save you extra work.
方法1:用FFmpeg序列输入匹配递增文件名
If you already have the TS files saved locally, or you're fetching them and saving with that media_[递增数] pattern, FFmpeg has built-in support for sequence-based inputs. Here's how to use it:
- Basic command (for variable-length numeric suffixes):
ffmpeg -start_number 3482026 -i media_%d.ts -c copy output.mp4-start_number: Sets the starting number in your sequence (matches your examplemedia_3482026.ts)%d: Acts as a placeholder for the incrementing decimal number (defaults to +1 per file, which perfectly fits your 6-second interval rule)
- If the numeric part has a fixed number of digits (like the 7-digit number in your example), use
%07dto ensure FFmpeg matches any leading zeros if they exist:ffmpeg -start_number 3482026 -i media_%07d.ts -c copy output.mp4 - The
-c copyflag copies video/audio streams directly without re-encoding—this is fast and preserves original quality.
方法2:更优方案——直接解析M3U8文件
Since you mentioned the site provides an M3U8 file where the last line points to the current TS segment, this is hands down the better approach. M3U8 is a playlist file made for streaming, and FFmpeg can automatically parse it to fetch and stitch all necessary segments:
ffmpeg -i "https://your-site-url/path/to/stream.m3u8" -c copy output.mp4
Why this is way better:
- No manual tracking: You don't have to worry about the starting number, increment steps, or missing segments—FFmpeg reads the M3U8 to know exactly which files to pull.
- Automatic encryption handling: If the stream is DRM-protected (common on many sites), FFmpeg will often handle decryption keys referenced in the M3U8, something you'd have to manage manually with individual TS files.
- Live stream friendly: If this is a live feed where the M3U8 updates with new segments, FFmpeg will keep pulling new chunks until you stop the process (Ctrl+C)—way easier than scripting a sequence grab for live content.
Quick Tips:
- If you run into access errors, some sites block direct FFmpeg requests—add a user-agent header with the
-headersflag to mimic a browser, e.g.:ffmpeg -headers "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" -i "https://your-site-url/path/to/stream.m3u8" -c copy output.mp4 - Avoid re-encoding unless you need to change the output format—
-c copyis always faster and preserves quality.
内容的提问来源于stack exchange,提问作者Andrey Kadnikov

