Python学习项目练习——m3u8视频下载
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">非常多</span>时候你想下载视频<span style="color: black;">无</span>下载按钮,<span style="color: black;">那样</span>你该怎么做呢?Python学习完以后你<span style="color: black;">能够</span><span style="color: black;">经过</span>Python下载这些视频。今天就分享一下m3u8视频<span style="color: black;">怎样</span>用Python下载。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/01ce7cb33c234654a8452a0c6d0fe1f4~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=km%2FOxfDwfIWswGOvVf13UoarK7k%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">查询</span>视频</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">找到视频的源文件,这是你要抓取文件的<span style="color: black;">第1</span>步。<span style="color: black;">此时</span>,你会<span style="color: black;">发掘</span>浏览器的<span style="color: black;">研发</span>者工<span style="color: black;">拥有</span>多么棒!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">电脑浏览器打开要下载电影的页面,按下F12打开浏览器<span style="color: black;">研发</span>工具,<span style="color: black;">选取</span>网络/Network,<span style="color: black;">这儿</span><span style="color: black;">能够</span>看到网站请求的所有数据</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/9cd487339e90431da9d9e1d1b13d136a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=l2OcJZJu5X9JWZdolr4aH1VheGE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">刷新页面,找到含有.m3u8的请求数据流,<span style="color: black;">能够</span>点击<span style="color: black;">研发</span>者工具栏,按ctrl+F<span style="color: black;">查询</span>.m3u8文件。<span style="color: black;">而后</span>打开标头/Headers,查看请求<span style="color: black;">位置</span>。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/6af1678ff9f942b19cbd95e9f883ea6a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=OYqstMr60E9ZDle2Kq54GU6XfTQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span><span style="color: black;">经过</span>预览/Response查看.m3u8文件<span style="color: black;">包括</span><span style="color: black;">那些</span>数据,将这些<span style="color: black;">位置</span>复制下来,<span style="color: black;">便是</span><span style="color: black;">咱们</span>要下载的视频数据,这些视频<span style="color: black;">通常</span>都是.ts格式的,<span style="color: black;">便是</span>视频片段。你<span style="color: black;">能够</span>一条一条下载下来,但既然<span style="color: black;">咱们</span>说了要用Python下载,<span style="color: black;">那样</span>,接下来的事情<span style="color: black;">咱们</span>用代码实现。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/a81d0a494e0a454a98aa227207d88b5b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=tvE45SxSz532RXTe%2Bec8cbIUpeg%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">ts视频下载</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>将预览内容Ctrl+A全选,<span style="color: black;">而后</span>复制,粘贴到新建的txt文件中,<span style="color: black;">而后</span>处理下载问题。</span></p><span style="color: black;">import</span> re
<span style="color: black;"># 读取文件,file<span style="color: black;">便是</span><span style="color: black;">保留</span>复制内容的txt文件</span>
<span style="color: black;"># lfile是<span style="color: black;">得到</span>复制视频的<span style="color: black;">次序</span>list_file</span>
<span style="color: black;"># dfile是要下载的视频链接 download_file</span>
<span style="color: black;"># url 是视频的url路径前缀</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">read</span><span style="color: black;">(file, lfile, dfile, url)</span>:</span>
<span style="color: black;">#读取复制内容</span>
<span style="color: black;">with</span> open(<span style="color: black;">f./<span style="color: black;">{file}</span></span>, <span style="color: black;">r+</span>, encoding=<span style="color: black;">utf-8</span>) <span style="color: black;">as</span> r:
<span style="color: black;"># 按行读取</span>
ts_file = r.readlines()
<span style="color: black;"># 正则模块,获取复制文件中的所有ts文件</span>
re_ts = re.compile(<span style="color: black;">r\w+\.ts</span>)
<span style="color: black;"># <span style="color: black;">倘若</span>ts文件有?后带的参数,<span style="color: black;">亦</span>要识别</span>
re_ts_data = re.compile(<span style="color: black;">r\w+\.ts?\w+.+|\w+\.ts</span>)
<span style="color: black;"># 正则模块,url正则</span>
re_ts_url = re.compile(<span style="color: black;">rhttp://\w+.+|https://\w+.+</span>)
<span style="color: black;"># 创建ts文件<span style="color: black;">次序</span>文件</span>
<span style="color: black;">with</span> open(<span style="color: black;">f<span style="color: black;">{lfile}</span></span>, <span style="color: black;">a</span>, encoding=<span style="color: black;">utf-8</span>) <span style="color: black;">as</span> w:
<span style="color: black;">for</span> i <span style="color: black;">in</span> ts_file:
<span style="color: black;">try</span>:
<span style="color: black;"># <span style="color: black;">查询</span>所有的ts文件名</span>
w.write(<span style="color: black;">ffile <span style="color: black;">{re.findall(re_ts, i)[<span style="color: black;">0</span>]}</span>\n</span>)
<span style="color: black;">except</span>:
<span style="color: black;">pass</span>
<span style="color: black;"># 创建ts下载文件</span>
<span style="color: black;">with</span> open(<span style="color: black;">f<span style="color: black;">{dfile}</span></span>, <span style="color: black;">a</span>, encoding=<span style="color: black;">utf-8</span>) <span style="color: black;">as</span> f:
<span style="color: black;">try</span>:
<span style="color: black;"># 创建下载文件时要在文件中添加url前缀</span>
f.write(<span style="color: black;">f<span style="color: black;">{url}</span><span style="color: black;">{re.findall(re_ts_data, i)[<span style="color: black;">0</span>]}</span>\n</span>)
<span style="color: black;">except</span>:
<span style="color: black;">pass</span>
read(<span style="color: black;">a.txt</span>, <span style="color: black;">list.txt</span>, <span style="color: black;">down.txt</span>, <span style="color: black;">https://www.xxx.com/</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是对复制内容的处理,方便后面的继续进行,<span style="color: black;">此时</span>候<span style="color: black;">咱们</span>得到了三个文件,一个是前面创建的txt文件,后面两个是程序运行时<span style="color: black;">得到</span>的文件。<span style="color: black;">重视</span>,<span style="color: black;">倘若</span>要创建新的下载视频,<span style="color: black;">这儿</span>要修改read()传入的参数。<span style="color: black;">由于</span>lfile和dfile的打开权限是a,再次执行会在原来文件内继续添加内容,会<span style="color: black;">导致</span>之前的内容重复。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/83fa5554815d41058583b9960e95f088~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=JVbVxOKtIO6Tk1uuufHTz64EN8M%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">import</span> os
<span style="color: black;">import</span> re
<span style="color: black;">import</span> wget <span style="color: black;"># 这个库<span style="color: black;">必须</span><span style="color: black;">经过</span>命令pip install wget下载</span>
<span style="color: black;"># 创建下载,dir是下载文件的文件夹名<span style="color: black;">叫作</span>,url是下载文件</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">url_down</span><span style="color: black;">(dir, url)</span>:</span>
<span style="color: black;">try</span>:
<span style="color: black;"># 创建文件夹</span>
os.mkdir(dir)
<span style="color: black;">except</span>:
<span style="color: black;">pass</span>
<span style="color: black;"># 下载文件</span>
wget.download(url, out=<span style="color: black;">f./<span style="color: black;">{dir}</span>/</span>)
<span style="color: black;">if</span> __name__ == <span style="color: black;">__main__</span>:
<span style="color: black;"># 读取down.txt文件中的ts视频的url</span>
<span style="color: black;">with</span> open(<span style="color: black;">down.txt</span>, <span style="color: black;">r+</span>, encoding=<span style="color: black;">utf-8</span>) <span style="color: black;">as</span> r:
txt_list = r.readlines()
<span style="color: black;"># 创建视频链接url的正则模块</span>
url_get = re.compile(<span style="color: black;">rhttp://\w+.+|https://\w+.+</span>)<span style="color: black;">for</span> i <span style="color: black;">in</span> txt_list:
<span style="color: black;"># <span style="color: black;">查询</span>ts的url</span>
k = re.findall(url_get, i)
<span style="color: black;"># <span style="color: black;">由于</span>文件中可能存在空行<span style="color: black;">或</span>非url行,<span style="color: black;">因此</span>加个校验</span>
<span style="color: black;">if</span> k == []:
<span style="color: black;">pass</span>
<span style="color: black;">else</span>:
<span style="color: black;"># 下载</span>
url_down(<span style="color: black;">视频</span>, k[<span style="color: black;">0</span>])<h1 style="color: black; text-align: left; margin-bottom: 10px;">ts视频转mp4</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当你执行完成后<span style="color: black;">发掘</span>会有<span style="color: black;">非常多</span>视频片段,不方便查看和<span style="color: black;">运用</span>,<span style="color: black;">因此</span>,<span style="color: black;">咱们</span>要把这些ts视频片段合并成一整段的mp4视频格式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>用到一个工具--ffmpeg,一款非常好用处理音视频的工具包。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/676db4d89d554eff8e8f0091512f75ac~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=fFoSXO47VwnhNimald5Odmw9LZ4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下载完成后,将文件解压,<span style="color: black;">而后</span>将bin文件的路径添加到系统环境中,命令行执行ffmpeg -version查看<span style="color: black;">是不是</span>安装成功。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/0242aaa56d0443a5ac1903125f64434c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=O%2BF4Ydm73BcQY4pmdKdMZrUNgpY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/286c46d2e11c42e0abfa2370803301d6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723897124&x-signature=%2BmyDQiqkOzy8QhEi74U9sNplEzA%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;"># 这是python一个终端操作工具</span>
<span style="color: black;">import</span> subprocess
<span style="color: black;">import</span> os
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">ts_mp4</span><span style="color: black;">(filename)</span>:</span>
ts_path = os.path.join(os.getcwd(), <span style="color: black;">视频/</span>)
<span style="color: black;">try</span>:
os.mkdir(<span style="color: black;">mp4视频</span>)
<span style="color: black;">except</span>:
<span style="color: black;">pass</span>mp4_path = os.path.join(os.getcwd(),<span style="color: black;">mp4视频/</span>)
cmdline = <span style="color: black;">f"ffmpeg -f concat -i <span style="color: black;">{ts_path}</span>list.txt -c copy <span style="color: black;">{mp4_path}</span><span style="color: black;">{filename}</span>.mp4"</span>
subprocess.call(cmdline, shell=<span style="color: black;">True</span>)
print(<span style="color: black;">f<span style="color: black;">{filename}</span>视频转换成功!</span>)
ts_mp4(<span style="color: black;">new</span>)
外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢! 百度seo优化论坛 http://www.fok120.com/ 你的见解独到,让我受益匪浅,非常感谢。
页:
[1]