linux: m3u8: 如何从 .m3u8 中列出的段手动下载和创建 mp4

Posted

技术标签:

【中文标题】linux: m3u8: 如何从 .m3u8 中列出的段手动下载和创建 mp4【英文标题】:linux: m3u8: how to manually download and create an mp4 from segments listed in .m3u8 【发布时间】:2018-11-11 11:13:08 【问题描述】:

我有一个 .m3u8 网址,即 http://example.com/test.m3u8

我可以通过ffmpeg命令下载:

ffmpeg -y -loglevel verbose -i "http://example.com/test.m3u8" -c copy -f mpegts test.mp4

但我正在尝试手动执行此操作:

我尝试了以下方法:

1) 下载m3u8文件:

wget -O test.m3u8 "http://example.com/test.m3u8"

2) 然后将每个片段下载到一个文件夹中:

aria2c -i test.m3u8

这会下载文件夹中的所有 .ts 文件。

3) 然后我将它们组合起来

cat *.ts > out.ts

4) 然后我尝试转换为 mp4:

$ ffmpeg -i out.ts -c:v libx264 outputfilename.mp4
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-shared --enable-version3 --enable-omx
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
out.ts: Invalid data found when processing input

我终于得到以下上述错误。 Invalid data found when processing input。我也听说这些文件是加密的。所以我不确定如何做到这一点。

下面是一个示例 .m3u8 文件:

#EXTM3U
#EXT-X-VERSION:5
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:5
#UPLYNK-SEGMENT: fadd0d418aea4c5da8b87feec9b0acbc,00000000,segment
#UPLYNK-KEY:https://content-aapm1.uplynk.com/check?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc
#EXT-X-PROGRAM-DATE-TIME:2018-06-01T05:25:50.630000+00:00
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000000
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000000.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000001
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000001.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000002
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000002.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000003
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000003.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000004
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000004.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000005
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000005.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000006
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000006.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000007
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000007.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000008
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000008.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000009
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000009.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000000A
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000000A.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000000B
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000000B.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000000C
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000000C.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000000D
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000000D.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000000E
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000000E.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000000F
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000000F.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000010
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000010.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000011
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000011.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000012
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000012.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000013
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000013.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000014
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000014.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000015
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000015.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000016
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000016.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000017
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000017.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000018
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000018.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x00000000000000000000000000000019
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G00000019.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000001A
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000001A.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000001B
#EXTINF:4.0960,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000001B.ts?x=0&si=0
#EXT-X-KEY:METHOD=AES-128,URI="https://content-aapm1.uplynk.com/check2?b=fadd0d418aea4c5da8b87feec9b0acbc&v=fadd0d418aea4c5da8b87feec9b0acbc&r=g",IV=0x0000000000000000000000000000001C
#EXTINF:3.6405,
https://stgec-ausw-tmp.uplynk.com/80C078/ausw/slices/fad/10b98e7c615f43a98b180d51797e74aa/fadd0d418aea4c5da8b87feec9b0acbc/G0000001C.ts?x=0&si=0
#EXT-X-ENDLIST

【问题讨论】:

我对 ts 文件了解不多,但我感觉cat *.ts 不会创建合适的 ts 文件,类似于 cat *.pdf 不会创建合适的文件pdf文件。尝试使用ffmpeg 连接您的文件。 连接 ts 段是正确的。 连接前必须解密。 如何解密 .ts 文件。 问:字幕呢?我从中流式传输 m3u8 的源网页有字幕,我想将这些字幕包含在我保存的文件中,以便以后可以选择在 VLC 中显示或不显示它们。如何将它们包含在此命令中? @SanthoshYedidi DeCSS? 【参考方案1】:

您可以为此使用 youtube-dl:

youtube-dl --list-formats M3U8_URL
youtube-dl -f FORMAT_CODE --hls-prefer-native M3U8_URL

大写字母的名称是占位符。 M3U8_URL 是 m3u8 文件的 URL。可用的FORMAT_CODEs 显示在第一个命令的输出左侧列中。

【讨论】:

这应该是最轻松的选择答案——它就像做梦一样!我在一个站点上成功下载了 Boxcast 流式传输的视频,包括查询参数上的签名密钥等(不要忘记将 URL 用引号括起来!)。这是一个仍在直播的视频。 youtube-dl 捕获了已经流式传输的内容。【参考方案2】:

关于cat 问题。你在考虑 BOM 吗? 所有 .ts 文件都以 BOM 开头,因此如果您对它们进行 cat 处理,它们就会损坏。

根据规范,您可以在媒体播放列表本身中找到所有必需的加密详细信息。

示例行:

#EXT-X-KEY:METHOD=AES-128,URI="http://example.com/path/to/resource.mp4/crypt.key?id=somekey"

这包含加密算法和密钥的 uri。 加密可以是每个列表或每个轨道。在您的情况下,它是每首曲目。

旁注: 我写了一些关于我使用 m3u8 / ts 的经验。也许它可以帮助你或给你一些新的想法:https://andreasvolkmann.github.io/posts/2018-04-27-m3u8-and-ts-segments/

【讨论】:

【参考方案3】:

ffmpeg 下载 .m3u8 并合并文件。

fmpeg -i "http://example.com/test.m3u8" -c copy -bsf:a aac_adtstoasc output.mp4

ffmpeg 版本

ffmpeg -h

ffmpeg 版本 4.4.1 版权所有 (c) 2000-2021 FFmpeg 开发者 使用 gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04) 构建 配置: --enable-pic --disable-doc --enable-debug --enable-runtime-cpudetect --enable-asm --enable-x86asm --enable-nonfree --enable-gpl --enable-version3 - -enable-ffmpeg --enable-ffplay --enable-ffprobe --enable-avcodec --enable-avdevice --enable-avformat --enable-avfilter --enable-postproc --enable-swresample --enable-swscale - -enable-avresample --enable-libaom --enable-libass --disable-avisynth --enable-bzlib --enable-libdav1d --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable- libfribidi --enable-iconv --enable-libilbc --enable-lzma --enable-libmp3lame --enable-libmodplug --enable-cuda --enable-nvenc --enable-nvdec --enable-cuvid --enable- ffnvcodec --enable-opencl --enable-opengl --enable-libopenh264 --enable-libopenjpeg --enable-openssl --enable-libopus --enable-sdl2 --enable-libsnappy --enable-libsoxr --enable- libspeex --disable-libssh --disable-libtensorflow --disable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libwebp - -enable-libx264 --enable-libx265 --disable-libxml2 --enable-zlib --pkg-config-flags=--static

【讨论】:

以上是关于linux: m3u8: 如何从 .m3u8 中列出的段手动下载和创建 mp4的主要内容,如果未能解决你的问题,请参考以下文章

ios m3u8格式的视频如何获取他的缩略图

如何使用 ffprobe 从 HLS/M3U8 文件中检测视频比特率

现在网上的视频很多都是m3u8格式的,它们用这种格式有啥好处?

ffmpeg进行视频切片,生成m3u8

从手机上下载的m3u8视频格式,怎么在电脑上播放?

ts文件、m3u8文件 key文件