python實(shí)現(xiàn)將m3u8視頻轉(zhuǎn)換成mp4的操作步驟
首先,網(wǎng)上獲取m3u8視頻
第一種,瀏覽器控制面板下,定位的Sources的tab, 在右側(cè)的XHR/fetch Breakpoints下添加過濾,如下圖所示,如果它訪問的網(wǎng)絡(luò)有對應(yīng)的過濾條件,會斷點(diǎn)暫停,此時(shí)可以獲取到ts文件
第二種,瀏覽器控制面板下,定位到Network的tab,下面子集的tab選中Fetch/XHR, 同時(shí)在搜索框輸入'ts',如下圖所示,重新刷新瀏覽器,如果接口訪問存在對應(yīng)的過濾條件,列表中會有對應(yīng)的訪問接口
其次,將m3u8視頻轉(zhuǎn)換成各個ts文件
第一步,我們通過python的requests請求m3u8鏈接,它會返回文件的內(nèi)容,m3u8的數(shù)據(jù)結(jié)構(gòu)如下圖所示。具體每個表示什么意思,網(wǎng)上都可以搜,不具體介紹。主要關(guān)心我們需要的各個ts。我們通過“\n”將內(nèi)容分開,會發(fā)現(xiàn)ts的前面是不帶“#”號的
根據(jù)自己的情況,ts的鏈接拼完整,通過requests將獲取到的內(nèi)容保存到本地。這里采用并行的方式(asyncio + aiohttp), 將所有的ts下載列表放在asyncio.gather中,aiohttp去請求遠(yuǎn)程ts,達(dá)到并行下載的效果
import requests import os import asyncio import aiohttp dirname = 'tsLib' if not os.path.exists(dirname): os.mkdir(dirname) headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'} async def download_ts(session, url, name): async with session.get(url, headers=headers) as response: data = await response.read() with open(os.path.join(dirname, name+'.ts'), mode='wb') as f: f.write(data) async def analysis_m3u8(data): filtered_content = data.split("\n") tasks = [] async with aiohttp.ClientSession() as session: for index, line in enumerate(filtered_content): if not line.startswith('#'): url = f'https://c-vod.hw-cdn.xiaoeknow.com/522ff1e0vodcq1252524126/9f5770b41397757889226643080/{line}' name = line.split('.')[0] print(name, 'namename') tasks.append(download_ts(session, url, name)) await asyncio.gather(*tasks) print("Downloads completed.") def get_m3u8(url): response = requests.get(url=url, headers=headers) m3u8_data = response.text return m3u8_data.strip() async def main(): m3u8_url = "https://c-vod.hw-cdn.xiaoeknow.com/522ff1e0vodcq1252524126/9f5770b41397757889226643080/playlist_eof.m3u8?sign=e6575d5fa9576eedbbc505cd53ca9ab5&t=66296146&us=xicXbvVMwn" m3u8_data = get_m3u8(m3u8_url) await analysis_m3u8(m3u8_data) asyncio.run(main())
最后,將ts文件合并為mp4文件
這里主要采用ffmpeg方式將ts合并為mp4, 但因?yàn)樯厦嫦螺d的順序按照ts名字來的,不一定有順序。所以我采用將m3u8獲取文件的順序、數(shù)組存儲,然后合并ts
import os import subprocess import requests headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'} def get_m3u8(url): response = requests.get(url=url, headers=headers) m3u8_data = response.text return m3u8_data.strip() def getFiles(): m3u8_url = "https://c-vod.hw-cdn.xiaoeknow.com/522ff1e0vodcq1252524126/9f5770b41397757889226643080/playlist_eof.m3u8?sign=e6575d5fa9576eedbbc505cd53ca9ab5&t=66296146&us=xicXbvVMwn" m3u8_data = get_m3u8(m3u8_url) filtered_content = m3u8_data.split("\n") files = [] for index, line in enumerate(filtered_content): if not line.startswith('#'): name = line.split('.')[0] files.append(f'{name}.ts') return files def merge_ts_to_mp4(ts_dir, output_mp4): files = getFiles() # Sort the filenames based on the numbers after the underscore ts_files = [f for f in files if f.endswith('.ts')] ts_paths = [os.path.join(ts_dir, f) for f in ts_files] print(ts_paths, '00999') # Generate a list of arguments for ffmpeg command ffmpeg_args = ['ffmpeg', '-i', 'concat:' + '|'.join(ts_paths), '-c', 'copy', output_mp4] # Run ffmpeg command subprocess.run(ffmpeg_args) print("Merged all .ts files into", output_mp4) # Example usage: ts_dir = 'tsLib' output_mp4 = 'merge_ts.mp4' merge_ts_to_mp4(ts_dir, output_mp4)
以上就是python實(shí)現(xiàn)將m3u8視頻轉(zhuǎn)換成mp4的操作步驟的詳細(xì)內(nèi)容,更多關(guān)于python m3u8轉(zhuǎn)mp4的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例
這篇文章主要介紹了python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例,是比較實(shí)用的技巧,需要的朋友可以參考下2014-10-10使用python批量修改文件名的方法(視頻合并時(shí))
這篇文章主要介紹了視頻合并時(shí)使用python批量修改文件名的方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Python實(shí)現(xiàn)的多線程同步與互斥鎖功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的多線程同步與互斥鎖功能,涉及Python多線程及鎖機(jī)制相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Python?pandas?DataFrame基礎(chǔ)運(yùn)算及空值填充詳解
pandas除了可以drop含有空值的數(shù)據(jù)之外,當(dāng)然也可以用來填充空值,下面這篇文章主要給大家介紹了關(guān)于Python?pandas?DataFrame基礎(chǔ)運(yùn)算及空值填充的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07