Skip to content

Instantly share code, notes, and snippets.

@dreness
Created November 14, 2016 03:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreness/bef154a9c97fc4429957add0b5dbe596 to your computer and use it in GitHub Desktop.
Save dreness/bef154a9c97fc4429957add0b5dbe596 to your computer and use it in GitHub Desktop.
Re-assemble HLS segments
1) If applicable, list available formats for this asset.
youtube-dl -F <url>
... then get URL to m3u8 for selected format
youtube-dl -f <format name> -g <url>
2) Download all the segments in the m3u8. Now you have a bag of transport stream segments.
The file names are probably increasing but not necessarily sequentially.
You can do the download with youtube-dl (which automatically re-assembles the results),
but it doesn't support parallel operation. You might find that parallelizing this download
is desirable, depending on where it's coming from.
3) Do a numeric sort (uzing zsh) of the downloaded file names, placing the result into a batch file for ffmpeg.
Edit the glob expression as needed to match only the clips you just downloaded.
cd <path of downloads>
rm concat.txt
echo highlight*ts(n) | tr ' ' '\n' | while read f ; do echo "file '$PWD/$f'" >> concat.txt ; done
Did you catch that? We're using the sort behavior from zsh "(n)", so switch to zsh temporarily if you don't already use it.
4) Pick an appropriate file extension for the media at hand.
Use ffprobe <segment path> to see what's inside:
ffprobe -loglevel 0 -show_format <file>
5) Use ffmpeg to concat a new file from the segments.
ffmpeg -f concat -safe 0 -i concat.txt -c copy <output file>
... we're using 'unsafe' mode here because ffmpeg considers absolute paths
in the concat.txt file to be *less* safe than relative paths, which is... backwards :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment