m3u8及TS文件下载解密:用FFmpeg解密合并m3u8中ts文件 1-3全集 Error when loading first segment ‘x.ts’

m3u8及TS文件下载解密:用FFmpeg解密合并m3u8中ts文件 1-3全集 Error when loading first segment ‘x.ts’

FFMPEG的基本命令
用FFMPEG来解密合并转化TS文件,网上有好多种写法,比如:

ffmpeg -protocol_whitelist “file,http,crypto,tcp” -i index.m3u8 -c copy out.ts

ffmpeg -allowed_extensions ALL -i index.m3u8 -c copy new.mp4

ffmpeg -allowed_extensions ALL -i index1.m3u8 -c copy -bsf:a aac_adtstoasc index.mp4

等。我直接用的中间这个就可以了。

String m3u8file = textBox5.Text;
var tool = @“ffmepg.exe”;
var command = ” -allowed_extensions ALL -i ” + m3u8file + ” -c copy ” + textBox8.Text + “\out.mp4 -y”;
using (var p = new Process())
{
RedirectExcuteProcess(p, tool, command, (s, e) => ShowDecodeInfo(m3u8file, e.Data));
}

遇到过的奇怪问题
在命令行调用FFMPEG时,出现错误
Unrecognized option ‘copy’.
Error splitting the argument list: Option not found

一直找不到原因。后来才发现是从网上直接拷的命令,其中的 -号是全角的,也就是在汉字输入状态下输入的,不是英文输入法下的 -。网上大家都是拷来拷去的,有问题,重新自行输入-符号,命令就可以正常执行。

M3U8文件格式
下载下来的m3u8打开后最典型的如下图:


详细的格式比较复杂,只就上述字段进行描述:

The basic commands of FFMPEG use FFMPEG to decrypt, merge and transform TS files. There are many ways to write on the Internet, such as: ffmpeg -protocol_whitelist “file,http,crypto,tcp” -i index.m3u8 -c copy out.tsffmpeg -allowed_extensions ALL -i index .m3u8 -c copy new.mp4ffmpeg -allowed_extensions ALL -i index1.m3u8 -c copy -bsf:a aac_adtstoasc index.mp4 etc. I just use the middle one directly.
String m3u8file = textBox5.Text;var tool = @”ffmepg.exe”;var command = “-allowed_extensions ALL -i” + m3u8file + “-c copy” + textBox8.Text + “\out.mp4 -y”;using (var p = new Process())
{RedirectExcuteProcess(p, tool, command, (s, e) => ShowDecodeInfo(m3u8file, e.Data));} Strange problems encountered when calling FFMPEG from the command line, an error Unrecognized option ‘copy’ appears.
Error splitting the argument list: Option not found The reason has not been found. Later, I discovered that the command was copied directly from the Internet, and the-sign was full-width, that is, it was entered in the state of Chinese character input, not the English input method -. Everyone on the Internet is copying it back and forth. If there is a problem, enter the-symbol again, and the command can be executed normally.
The most typical m3u8 downloaded in the M3U8 file format is as shown in the figure below: The detailed format is more complicated, and only the above fields are described: EXTM3U start identification. The first line of each M3U file must be this.
EXT-X-VERSION: The 3EXT-X-VERSION tag indicates the adaptability of the playlist version. Playlist files, their associated media and servers must comply with all regulations of the latest version.
The use of IV in the EXT-X-KEY tag must specify that the version number of the protocol is 2 (what is the IV, see the EXT-X-KEY part below)
The value of the floating-point number used in EXTINF must specify the protocol version number is 3 Use EXT-X-BYTERANGE and EXT-X-IFRAME-ONLY flags must specify the version number of the protocol is 4 Use SAMPLE-AES in the EXT-X-KEY tag The version of the protocol must be specified as 5EXT-X-TARGETDURATION: S is an integer in seconds.
The tag specifies the maximum duration of the media file, and the duration defined in the EXTINF tag of the media file in the play file list must be less than or equal to the duration specified by the tag. The tag must appear once in the playlist file.
EXT-X-MEDIA-SEQUENCE defines the sequence number of the first file in the current m3u8 file. It seems useless.
EXT-X-KEY defines the encryption method and the url of the key file, which is used to obtain the 16bytes key file to decode the ts file.
Attribute: METHOD, encryption method, generally AES-128URL, the address of the key file IV, AES decryption algorithm, there can be two parameters Key and IV, IV is the initialization variable of the AES algorithm CBC encryption mode, the default is 16 0 , You can set other values.
EXTINF specifies the duration of each media segment (ts), this is only valid for the following URI, which is the specific video file.
EXT-X-ENDLIST indicates the end of the m3u8 file. The live m3u8 does not have this tag.
Tool download TSMatser is a download decryption tool for m3u8 and TS files, CKplayer video files.
The tool can automatically parse key files and ts files.
Other tools to download ts can use other tools to download ts files, such as the resource sniffer of the travel browser, after sniffing, filtering and downloading ts files.
You can also use VLC Player to input the m3u8 address directly, then you can play and download.
Multi-threaded download TS file aria2c download TS file aria2c is a utility for downloading files. Support HTTP(S), FTP, SFTP, BitTorrent, and Metalink protocols. We can call aria2c to download specific TS files.
Specific c# code: var url = this.cururlpath + curdownfile;var tool = @”aria2c.exe”;var fi = new FileInfo(textBox6.Text + “\” + textBox9.Text + “\” + curdownfile);var command = “-c -s 10 -x 10 –file-allocation=none –check-certificate=false -d” + fi.DirectoryName + “-o” + fi.Name + “” + url;//Console. WriteLine(command);using (var p = new Process())
{undefinedRedirectExcuteProcess(p, tool, command, (s, e) => ShowDownInfo(i,url, e.Data));} Based on the returned output in the ShowDownInfo callback function, make a corresponding judgment: if (a == null ) return;const string re1 = “.*?”; // Non-greedy match on fillerconst string re2 = “(\\(.*\\))”; // Round Braces 1var r = new Regex(re1 + re2 , RegexOptions.IgnoreCase | RegexOptions.Singleline);var m = r.Match(a);if (m.Success)
{var rbraces1 = m.Groups[1].ToString().Replace(“(“, “”).Replace(“)”, “”).Replace(“%”, “”).Replace(“s” , “0”);if (rbraces1 == “OK”)
{listView1.BeginUpdate();listView1.Items[itemidx].SubItems[1].Text = “Downloaded”;listView1.EndUpdate();this.Invoke(new Action(() => {label20.Text = “Already Download “+ (downfilescnt + 2) + “/” + listView1.Items.Count; }));}//label20.Text = DateTime.Now.ToString().Replace(“/”, “-“) +” “+ url +” Download progress: “+ rbraces1 + “%”; Multi-threaded downloading In order to improve the download speed, a thread pool is used to call the downloader.
For (int i = 0; i <thStepCount; i++)
{undefinedthStep[i] = new Thread(threaddownloadfiles);thStep[i].Name = i.ToString();thStep[i].Start();} Thread synchronization problem. Multiple threads simultaneously call aria2c to the m3u8 file All TS files are downloaded. In order to ensure that the work of each thread is not repeated or omitted, a synchronization mechanism is required. At first, I wanted to use lock to achieve it. I found it more troublesome. Later, I changed to a more clumsy way to count the total number of TS files and follow the threads. Numbers are allocated, and each completes the download tasks within their own allocation range: if (Thread.CurrentThread.Name.Equals(“0”)) //Files processed by thread 1 {undefinedstartpos = 0; endpos = listView1.Items.Count / 3 ;}else if (Thread.CurrentThread.Name.Equals(“1”)) //File processed by thread 2 {startpos = listView1.Items.Count / 3; endpos = listView1.Items.Count * 2 / 3;}else if (Thread.CurrentThread.Name.Equals(“2″)) //Files processed by thread 3 {startpos = listView1.Items.Count * 2 / 3; endpos = listView1.Items.Count;}for (int i = startpos ; i <endpos; i++)
{Undefined//Call the downloaded function;} Tool Download TSMatser is a m3u8 and TS file, CKplayer video file download decryption tool. When downloading key files and ts files, multi-threading technology is used, and aria2c is called for downloading.

 

EXTM3U
开始标识。每个M3U文件第一行必须是这个。

EXT-X-VERSION:3
EXT-X-VERSION标签指出了播放列表版本的适应性。播放列表文件、其关联的媒体和服务器必须遵守最新版本的所有规定。
在EXT-X-KEY标记中使用IV必须指定协议的版本号是2(IV是啥,看后面的EXT-X-KEY部分)
在EXTINF中使用浮点数的值必须指定协议版本号是3
使用 EXT-X-BYTERANGE 和 EXT-X-IFRAME-ONLY标记必须指定协议的版本号是4
在EXT-X-KEY标记中使用SAMPLE-AES必须指定协议的版本是5

EXT-X-TARGETDURATION:
S是一个以秒为单位的整数。
该标签指定了媒体文件持续时间的最大值,播放文件列表中的媒体文件在EXTINF标签中定义的持续时间必须小于或者等于该标签指定的持续时间。该标签在播放列表文件中必须出现一次。

EXT-X-MEDIA-SEQUENCE
定义当前m3u8文件中第一个文件的序列号。好像没啥用。

EXT-X-KEY
定义加密方式和key文件的url,用于取得16bytes的key文件解码ts文件。
属性:
METHOD,加密方式,一般是AES-128
URL, 密钥文件的地址
IV,AES解密算法中,可以有 Key 和 IV 两个参数,IV是AES算法CBC加密模式的初始化变量,默认是16个0,可以设置其他值。

EXTINF
指定每个媒体段(ts)的持续时间,这个仅对其后面的URI有效,后面的URI就是具体的视频文件。

EXT-X-ENDLIST
表明m3u8文件的结束。直播的m3u8没有该tag。

工具下载
TSMatser是一个m3u8及TS文件、CKplayer视频文件下载解密工具。
工具可自动解析key文件和ts文件。

 

其他工具下载ts
可以用其他工具下载ts文件,比如遨游浏览器的资源嗅探器,嗅探后筛选下载ts文件。

也可以用VLC Player直接输入m3u8地址,就可以播放下载了。

 

多线程下载TS文件
aria2c下载TS文件
aria2c 是一个用来下载文件的实用程序。支持HTTP(S), FTP, SFTP, BitTorrent,和Metalink协议。我们可以调用aria2c来下载具体的TS文件。

具体c#代码:
var url = this.cururlpath + curdownfile;
var tool = @“aria2c.exe”;
var fi = new FileInfo(textBox6.Text + “\” + textBox9.Text + “\” + curdownfile);
var command = ” -c -s 10 -x 10 –file-allocation=none –check-certificate=false -d ” + fi.DirectoryName + ” -o ” + fi.Name + ” ” + url;
//Console.WriteLine(command);
using (var p = new Process())
{undefined
RedirectExcuteProcess(p, tool, command, (s, e) => ShowDownInfo(i,url, e.Data));
}

在ShowDownInfo回调函数中基于返回的输出,进行相应的判断:
if (a == null) return;

const string re1 = “.*?”; // Non-greedy match on filler
const string re2 = “(\\(.*\\))”; // Round Braces 1

var r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
var m = r.Match(a);
if (m.Success)
{
var rbraces1 = m.Groups[1].ToString().Replace(“(“, “”).Replace(“)”, “”).Replace(“%”, “”).Replace(“s”, “0”);
if (rbraces1 == “OK”)
{
listView1.BeginUpdate();
listView1.Items[itemidx].SubItems[1].Text = “已下载”;
listView1.EndUpdate();
this.Invoke(new Action(() => { label20.Text = “已下载” + (downfilescnt + 2) + “/” + listView1.Items.Count; }));

}
//label20.Text = DateTime.Now.ToString().Replace(“/”, “-“) + ” ” + url + ” 下载进度:” + rbraces1 + “%”;

点此翻页下一页

发表评论

后才能评论