1. 程式人生 > >m3u8文件下載合並的一種方法

m3u8文件下載合並的一種方法

pac quest created -s https packages 2.0 print ams

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 14 15:09:14 2018
@author: Y
"""
 
 
import requests
import json
 
 
#m3u8的文件路徑
path = input("Enter m3u8 file path:").replace(‘\\‘,‘/‘)
print(path)
file = open(path,‘r‘)
operation = input("是否要加上前綴?y/n\n").strip()
pre_link = ‘‘
if operation == ‘y‘:
    pre_link = input("請輸入前綴:").strip()
links = []
for i in file:
    if ‘#‘ not in i:
        i = i.strip()
        links.append(pre_link+i)
file.close()
l = len(links)
print("總共有%d個片段..."%l)
length = len(str(len(links)))
n = 0
txt = ""
for link in links:
    n = n + 1
    print("還剩%d個片段未下載..."%(l-n))
    if len(str(n)) < length:
        name = ‘0‘*(length-len(str(n))) + str(n) + ".ts"
    else:
        name = str(n)+".ts"
    txt = txt + "file \‘" + name + "\‘\n"
    jsonreq = json.dumps({‘jsonrpc‘:‘2.0‘, ‘id‘:1,
               ‘method‘:‘aria2.addUri‘,
               ‘params‘:[[link],{"out":name,"split":"5","max-connection-per-server":"16","seed-ratio":"0"}]})
    c = requests.post(‘http://localhost:6800/jsonrpc‘, jsonreq)
file = open("E:\\aria2data\\filelist.txt","w")
file.write(txt)
file.close()

  

生成的ts文件用 ffmpeg 合並,命令行輸入:ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.ts

https://xyne.archlinux.ca/projects/python3-aria2jsonrpc/

https://aur.archlinux.org/packages/python3-aria2jsonrpc/

https://xyne.archlinux.ca/projects/python3-aria2jsonrpc/src/

m3u8文件下載合並的一種方法