欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)將m3u8視頻轉(zhuǎn)換成mp4的操作步驟

 更新時(shí)間:2024年05月30日 08:49:40   作者:芹小妹_Jessica  
m3u8 是一種基于文本的媒體播放列表文件格式,通常用于指定流媒體播放器播放在線媒體流,MP4是一種基于MPEG-4 Part 12(2015)和MPEG-4 Part 14標(biāo)準(zhǔn)的數(shù)字多媒體容器格式,本文將給大家介紹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中Django的路由配置詳解

    Python中Django的路由配置詳解

    這篇文章主要介紹了Python中Django的路由配置詳解,Python下有許多款不同的?Web?框架,Django是重量級選手中最有代表性的一位,許多成功的網(wǎng)站和APP都基于Django,需要的朋友可以參考下
    2023-07-07
  • Python實(shí)現(xiàn)對adb命令封裝

    Python實(shí)現(xiàn)對adb命令封裝

    這篇文章主要介紹了Python實(shí)現(xiàn)對adb命令封裝,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例

    python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例

    這篇文章主要介紹了python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例,是比較實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • 關(guān)于Python操作Excel的基本方法

    關(guān)于Python操作Excel的基本方法

    這篇文章主要介紹了關(guān)于Python操作Excel的基本方法,Python是一種功能強(qiáng)大的編程語言,可以用于許多任務(wù),包括處理Excel文件,需要的朋友可以參考下
    2023-05-05
  • 深入理解Python虛擬機(jī)中的反序列化pyc文件

    深入理解Python虛擬機(jī)中的反序列化pyc文件

    再這篇文章中我們將主要對?Code?Object?進(jìn)行分析,并且詳細(xì)它是如何被反序列化的,通過本篇文章我們將能夠把握整個?pyc?文件結(jié)構(gòu),感興趣的可以了解一下
    2023-05-05
  • 使用python批量修改文件名的方法(視頻合并時(shí))

    使用python批量修改文件名的方法(視頻合并時(shí))

    這篇文章主要介紹了視頻合并時(shí)使用python批量修改文件名的方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python實(shí)現(xiàn)屏幕錄制功能的代碼

    Python實(shí)現(xiàn)屏幕錄制功能的代碼

    這篇文章主要介紹了Python實(shí)現(xiàn)屏幕錄制功能,本文給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python 3.7.0 下pillow安裝方法

    python 3.7.0 下pillow安裝方法

    這篇文章主要為大家詳細(xì)介紹了python 3.7.0 下pillow的安裝方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Python實(shí)現(xiàn)的多線程同步與互斥鎖功能示例

    Python實(shí)現(xiàn)的多線程同步與互斥鎖功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的多線程同步與互斥鎖功能,涉及Python多線程及鎖機(jī)制相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Python?pandas?DataFrame基礎(chǔ)運(yùn)算及空值填充詳解

    Python?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

最新評論