原理分析
ChinaDaily 音频文件是采用外链直接进行的分发,使用标准的html5 audio控件进行渲染,因此可以直接右击另存为下载。ChinaDaily 视频是采用m3u8切片的模式分发视频流,经过测试并没有加密,因此想要下载视频,关键是获取m3u8文件。
音频下载
音频下载很简单,采用图文的方式向大家说明。以 每日新闻播报(February 11) 为例,打开新闻页面,直接在音频控件处右击,选择
另存音频为
即可将音频保存下来。视频下载
对于m3u8文件的解析及视频下载,这里采用开源免费的 N_m3u8DL-RE + ffmpeg 来实现。 演示系统:windows 11 22H2(限于篇幅,macOS教程我未来可能会另开一篇帖子)
ffmpeg下载地址:https://github.com/BtbN/FFmpeg-Builds/r ... 64-gpl.zip
N_m3u8DL-RE下载地址:https://github.com/nilaoda/N_m3u8DL-RE/ ... 221017.zip
因为国内的网络长城,如果你没法访问Github,请看下面的教程(点三角形展开)
windows host 修改教程
在C:\Windows\System32\drivers\etc
目录下的hosts
文件中添加如下字段
代码: 全选
140.82.121.3 github.com
185.199.111.133 raw.githubusercontent.com
如果保存遇到权限问题,可以先将host文件拷贝到桌面,修改桌面上的拷贝,修改完后再将其复制并粘贴覆盖
C:\Windows\System32\drivers\etc
目录下的hosts
文件https://cloud.bobmaster.cyou/s/oZ2J3bKaNbmScdE
1.准备工作
准备工作主要是下载工具和进行一些基础的配置,只需要配置一次,未来不需要再次经历该流程,直接进入视频下载步骤即可下载
ffmpeg
和 N_m3u8DL-RE
,将它们解压,然后将 ffmpeg
添加至系统环境变量PATH
中
环境变量PATH是指该路径下的程序可以通过其名称直接被调用而不需要指定其完整的路径
为了节约篇幅,如果你不知道如何添加,可以查看上面的视频教程
2.视频资源m3u8文件获取
以 Key takeaways from the report to 20th congress 这篇新闻为例子,介绍其视频下载方法- 第一步,在视频控件处右击,点击
审查元素(inspect)
找到iframe
标签,双击src
,复制其内容,大致长这样代码: 全选
//v-hls.chinadaily.com.cn/player/player.html?src=//v-hls.chinadaily.com.cn/stream/606630/1779cafe-27f1-4e88-b509-1d4ac9d28c22/bab4ffbf-f924-4a1f-a757-33aae4aff94a_h.m3u8&p=//img2.chinadaily.com.cn/images/202210/17/634d3358a310fd2beca72d34.jpeg&l=en&c=63313848a310fd2b29e79bfc&s=WS634d3358a310fd2b29e7cfcd&d=20221017184940&t=Key+takeaways+from+the+report+to+20th+congress
- 第二步,在浏览器新开一个页面,打开上面复制的src地址
按F12
键打开浏览器开发者控制台,选中Network
,然后刷新一下页面,找到一个以mp4.m3u8
结尾的请求,我这里获取到的是代码: 全选
http://v-hls.chinadaily.com.cn/stream/606630/1779cafe-27f1-4e88-b509-1d4ac9d28c22/2000000_720_1280_38e3e8ad-6ca6-410b-919f-59bcc3c52c69.mp4.m3u8
3.视频下载
进入到N_m3u8DL-RE
这个工具解压后的文件夹里,在文件夹路径框里输入 cmd
然后回车调出windows的终端输入如下命令下载视频
代码: 全选
N_m3u8DL-RE.exe "m3u8地址" --save-name "你想将视频保存的名称(自己任意设定)"
# 在本例中使用的是如下命令
N_m3u8DL-RE.exe "http://v-hls.chinadaily.com.cn/stream/606630/1779cafe-27f1-4e88-b509-1d4ac9d28c22/2000000_720_1280_38e3e8ad-6ca6-410b-919f-59bcc3c52c69.mp4.m3u8" --save-name "Key takeaways from the report to 20th congress"
该软件还有其它客制化命令,可以使用参数
-?/-h/--help
查看
代码: 全选
N_m3u8DL-RE.exe --help
Description:
N_m3u8DL-RE (Beta version) 20221017
Usage:
N_m3u8DL-RE <input> [options]
Arguments:
<input> Input Url or File
Options:
--tmp-dir <tmp-dir> Set temporary file directory
--save-dir <save-dir> Set output directory
--save-name <save-name> Set output filename
--base-url <base-url> Set BaseURL
--thread-count <number> Set download thread count [default: 8]
--download-retry-count <number> The number of retries when download segment error [default: 3]
--auto-select Automatically selects the best tracks of all types [default: False]
--skip-merge Skip segments merge [default: False]
--skip-download Skip download [default: False]
--check-segments-count Check if the actual number of segments downloaded matches the expected
number [default: True]
--binary-merge Binary merge [default: False]
--del-after-done Delete temporary files when done [default: True]
--write-meta-json Write meta json after parsed [default: False]
--append-url-params Add Params of input Url to segments, useful for some websites, such as
kakao.com [default: False]
-mt, --concurrent-download Concurrently download the selected audio, video and subtitles [default:
False]
-H, --header <header> Pass custom header(s) to server, Example:
-H "Cookie: mycookie" -H "User-Agent: iOS"
--sub-only Select only subtitle tracks [default: False]
--sub-format <SRT|VTT> Subtitle output format [default: VTT]
--auto-subtitle-fix Automatically fix subtitles [default: True]
--ffmpeg-binary-path <PATH> Full path to the ffmpeg binary, like C:\Tools\ffmpeg.exe
--log-level <DEBUG|ERROR|INFO|OFF|WARN> Set log level [default: INFO]
--ui-language <en-US|zh-CN|zh-TW> Set UI language
--urlprocessor-args <urlprocessor-args> Give these arguments to the URL Processors.
--key <key> Pass decryption key(s) to mp4decrypt/shaka-packager. format:
--key KID1:KEY1 --key KID2:KEY2
--key-text-file <key-text-file> Set the kid-key file, the program will search the KEY with KID from the
file.(Very large file are not recommended)
--decryption-binary-path <PATH> Full path to the tool used for MP4 decryption, like C:\Tools\mp4decrypt.exe
--use-shaka-packager Use shaka-packager instead of mp4decrypt to decrypt [default: False]
--mp4-real-time-decryption Decrypt MP4 segments in real time [default: False]
-M, --mux-after-done <OPTIONS> When all works is done, try to mux the downloaded streams. Use "--morehelp
mux-after-done" for more details
--custom-hls-method <METHOD> Set HLS encryption method
(AES_128|AES_128_ECB|CENC|CHACHA20|NONE|SAMPLE_AES|SAMPLE_AES_CTR|UNKNOWN)
--custom-hls-key <FILE|HEX|BASE64> Set the HLS decryption key. Can be file, HEX or Base64
--custom-hls-iv <FILE|HEX|BASE64> Set the HLS decryption iv. Can be file, HEX or Base64
--use-system-proxy Use system default proxy [default: True]
--custom-proxy <URL> Set web request proxy, like http://127.0.0.1:8888
--live-perform-as-vod Download live streams as vod [default: False]
--live-real-time-merge Real-time merge into file when recording live [default: False]
--live-keep-segments Keep segments when recording a live (liveRealTimeMerge enabled) [default:
True]
--live-record-limit <HH:mm:ss> Recording time limit when recording live
--live-wait-time <SEC> Manually set the live playlist refresh interval
--mux-import <OPTIONS> When MuxAfterDone enabled, allow to import local media files. Use
"--morehelp mux-import" for more details
-sv, --select-video <OPTIONS> Select video streams by regular expressions. Use "--morehelp select-video"
for more details
-sa, --select-audio <OPTIONS> Select audio streams by regular expressions. Use "--morehelp select-audio"
for more details
-ss, --select-subtitle <OPTIONS> Select subtitle streams by regular expressions. Use "--morehelp
select-subtitle" for more details
--morehelp <OPTION> Set more help info about one option
--version Show version information
-?, -h, --help Show help and usage information
Bonus
如果你想更纯粹一些,不想用N_m3u8DL-RE
,其实 ffmpeg
可以直接胜任视频的下载工作,在终端cmd
里使用下面的命令即可。
代码: 全选
ffmpeg -i "m3u8地址" -c copy "视频保存的名称.{格式}"
# 化用于今天的教程中的例子就是
ffmpeg -i "http://v-hls.chinadaily.com.cn/stream/606630/1779cafe-27f1-4e88-b509-1d4ac9d28c22/2000000_720_1280_38e3e8ad-6ca6-410b-919f-59bcc3c52c69.mp4.m3u8" -c copy "Key takeaways from the report to 20th congress.mp4"