欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HLS视频相关操作入门(ffmpeg)

程序员文章站 2022-07-02 10:30:52
...

mp4转ts

ffmpeg -i inputfile   -codec copy -bsf h264_mp4toannexb outputfile

ts转MP4

ffmpeg -i inputfile  -acodec copy -vcodec copy -bsf aac_adtstoasc  outputfile

hls(m3u8)下载并转mp4

ffmpeg -i https://xxx.xxx.xxx/xxx.m3u8 -c copy -absf aac_adtstoasc output.mp4

ts(多个ts文件)转mp4

ffmpeg -y -f concat -i mp4list.txt -c copy asddaas.mp4

其中,mp4list.txt文件内容格式如下:

mp4list.txt文件内容: 
file “1.ts” 
file “2.ts” 
file “3.ts”

下载hls中的多个ts片段

#获取所有ts片段的url
def getAllUrl(url):
    url_list = []
    html = requests.get(url)
    file_url = html.text.split('\n')
    for url in file_url:
        if len(url) > 0 and url[0] != '#':
            url_list.append(url)
    return url_list
 
file_path = './' 
file_url = getAllUrl(url):  
base_url = '/'.join(url.split('/')[0:-1])
for u in file_url:
        url = base_url + '/' + u
        print('url', url)
        if os.path.exists(file_path + '/' + u):
            continue

        with open(file_path + '/' + u, 'wb') as f:
            f.write(requests.get(url).content)
        f.close()

多个ts片段合并

[shell]

for i in `ls *.ts`; do cat $i >> output.ts; done

[python]

import subprocess
read_file = ["1.ts","2.ts","3.ts"]
for i in read_file:
    str_c = 'cat ' +i+  ' >> ' + outputfile.strip()
    print(str_c)
    subprocess.getstatusoutput(str_c)

参考文献:
https://blog.csdn.net/weixin_34296641/article/details/91925392

<<<未完待续

相关标签: 音视频开发