怎么下载加密ts流的视频

这边无法显示图片,去我下面这个文章看有图片版的吧:

怎么下载加密ts流的视频-CSDN博客

以此网站如下的电影《2012》为例:
![image.png](https://upload-images.jianshu.io/upload_images/23788354-0c052ccab426c0bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


在这个网站上面,电影2012是以一系列几秒的ts格式来播放的,所以没办法直接复制视频地址来下载整部电影。看如下截图:

![image.png](https://upload-images.jianshu.io/upload_images/23788354-c883a8d6343ee5f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


并且,每段ts还是加密的,单独下载ts文件是无法播放的,需要解密,如下图:

![image.png](https://upload-images.jianshu.io/upload_images/23788354-9b785555aecc5207.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

那要怎样才能下载完整的解密后的视频呢?下面分几步进行说明。

**1、首先,获取该电影所有的ts列表,和加密方式及密钥:**

要用chrome浏览器打开该网址,然后右击,点击检查,然后重新刷新页面,然后根据如下截图查看:

![image.png](https://upload-images.jianshu.io/upload_images/23788354-2fc04745bcf0cd04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

点击“index.m3u8”这个请求,然后根据如下截图:
![image.png](https://upload-images.jianshu.io/upload_images/23788354-8292ff31bc79b04d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

能够得出该电影的所有ts列表,并且加密方式是“AES-128”,密钥是enc.key的请求中,iv是16字节长度的0 。 现查看enc.key请求如下:
![image.png](https://upload-images.jianshu.io/upload_images/23788354-1ad98e270cf0d87d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

发现是乱码(有些网站不是乱码,而是字符串)。乱码是因为该密钥是二进制的,需要用查看hex工具来获取16进制的密钥。

先下载该“enc.key”到本地,然后用hex工具查看16进制值。mac系统可以用如下查看:

![image.png](https://upload-images.jianshu.io/upload_images/23788354-f0579896f1bfd722.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

可以得出该密钥的16进制为:7be5d74d56af87838c3b98f1a2febf8f

**2、根据ts列表,用php来实现多进程快速下载**

下载所有ts文件有很多方法,可以手动一个个下载,但是因为太多,所以这个方法会比较麻烦。可以用php脚本来快速下载。

创建个1.php文件,用来下载ts文件。写入如下内容:

```php
<?php

function my_file_get_contents($url) {
    $arrContextOptions = [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ]
    ];
    return file_get_contents($url, false, stream_context_create($arrContextOptions));
}

for ($i=$argv[1]; $i <= $argv[2]; $i++) { 
    echo $i.'...'.PHP_EOL;
    $f = $i.'.ts';
    if (file_exists($f)) {
        continue;
    }
        // 下面的链接要改成“index.m3u8”这里面相对应的ts链接
    $data = my_file_get_contents('https://hnts.ymuuy.com:65/hls/200/20240110/2077/plist'.$i.'.ts');
    file_put_contents($f, $data);
}
```

然后,再创建个2.php文件,用来创建下载命令。写入如下内容:

```php
<?php

// 882要改成改成“index.m3u8”这里面最大数字的ts链接后的数字
for ($i=1; $i<=882; $i+=20) {
    $tmp = $i+20;
    if ($tmp > 882) {
        $tmp = 882;
    }
    echo 'php 1.php '.$i.' '.$tmp.' &'.PHP_EOL;
}
```

然后,运行如下命令:
![image.png](https://upload-images.jianshu.io/upload_images/23788354-4481e5cf495bafd1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

生成了可以多进程下载ts文件的命令行,然后复制生成的命令,在终端运行如下:
![image.png](https://upload-images.jianshu.io/upload_images/23788354-4c9aa5894204b74f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

可以看到,已经在快速下载了,分为了882/20=44个进程来同时快速下载。

可以用如下命令来查看下载进度:

```shell
while true
do
du -sh `pwd`; ls |wc -l;sleep 1;
done
```

显示如下:

![image.png](https://upload-images.jianshu.io/upload_images/23788354-034d3b7c9f0a5669.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

会显示出当前下载的大小,和下载的总ts数。

注意,全部都下载完后,要查看下有没有大小为0的ts文件,这些是下载失败的文件,删除后,重新运行下下载命令即可。

**3、所有文件都下载完后,要开始解密并合并了**

同样也是用php脚本来解密,保存下面脚本为decrypt.php:
```php
<?php

// 如果“enc.key”的密钥是二进制的话,就用下面这行
$key = hex2bin("7be5d74d56af87838c3b98f1a2febf8f");
// 如果“enc.key”的密钥是字符串的话,就用下面这行
// $key = 'Cibz2Dp3bCnzlmVx';

// 原样复制“index.m3u8”里面的IV的0x后面的部分
$iv = hex2bin("00000000000000000000000000000000");

$decrypted_file = 'output.ts'; // 最终要保存的文件

// 882改为ts总数
for ($i=1; $i<=882; $i++) {
    echo $i,'...',PHP_EOL;
    $encrypted_file = $i.'.ts';
    $data = file_get_contents($encrypted_file);
    $decrypted_data = openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
    file_put_contents($decrypted_file, $decrypted_data, FILE_APPEND);
}

echo "解密成功,已保存为:".$decrypted_file;

```

运行如下命令:

![image.png](https://upload-images.jianshu.io/upload_images/23788354-8f05e4f41f0a52f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/23788354-bb0a29175a6a4175.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

这样,就成功的解密并合并为了output.ts文件,用支持ts的播放器就可以播放此电影了。

有问题这边留言探讨下~
 

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值