程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python realizes m3u8 to mp4

編輯:Python

python Realization m3u8 turn mp4

Need to use Two versions Of ffmpeg, Document and original code succeed in inviting sb. Project address Self taking
https://gitee.com/z2322739526/m3u8
Main solution ts Change suffix mp4 after Unable to upload online disk to play The problem of , So it needs to be handled completely
tomp4.py Time consuming computer , Transcoding speed about 6 minute Total processed 5 minute video
The complete code is as follows
m3u8.py

import requests
import re
import os
import urllib.parse
import time
import shutil
start_time = time.time()
headers = {

'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
}
m3u8_url = input("m3u8_url:")
# url decode 
m3u8_url = urllib.parse.unquote(m3u8_url)
r_url = '.*&vurl=(.*ver=4)'
url_list = re.findall(r_url,m3u8_url)
if len(url_list) != 0:
m3u8_url = url_list[0]
if m3u8_url == '1':
m3u8 = open('14.m3u8').read()
else:
m3u8 = requests.get(m3u8_url,headers = headers).text
r_m3u8 = ',\n(.*?)\n#'
# re.S The whole match 
ts_all = re.findall(r_m3u8,m3u8,re.S)
print(ts_all[:10])
print(' common %d individual ts file '%len(ts_all))
# Before deleting ts
if os.path.exists('./ts'):
shutil.rmtree('./ts')
os.mkdir('./ts')
if not os.path.exists("./ts"):
os.mkdir("./ts")
num = 0
for ts_2 in ts_all:
try:
ts = requests.get(ts_2,headers = headers).content
np = (len(str(len(ts_all)))-len(str(num)))*'0'+str(num)
with open('./ts/%s.ts'%np,'wb') as fp:
fp.write(ts)
print('%s.ts save'%np)
num += 1
except:
# https://jx.parwix.com:4433/player/?url=https://www.iqiyi.com/v_dql4i2lz0c.html?vfm=2008_aldbd&fv=p_02_01
# Resolve system specific links 
ts_2 = 'https://211.99.101.171:4433' + ts_2
ts = requests.get(ts_2,headers = headers).content
np = (len(str(len(ts_all)))-len(str(num)))*'0'+str(num)
with open('./ts/%s.ts'%np,'wb') as fp:
fp.write(ts)
print('%s.ts save'%np)
num += 1
end_time = time.time()
print(' Download complete , Total time :',end_time-start_time)
# https://sod.bunediy.com/20211217/njayYgPt/index.m3u8 

tomp4.py

import sys
import os
import time
start_time = time.time()
def getmax(file_dir):
for root, dirs, files in os.walk(file_dir):
return len(files[0])-3
break
print(sys.path[0])
start = int(input(' Please enter the starting number :'))
num = len(os.listdir("./ts"))
fmax = getmax("./ts")
print(' Maximum length ', fmax)
for i in range(start,num):
name = (fmax - len(str(i)))*'0' + str(i)
print(' Dealing with ', name)
os.system("ffmpeg-old -i " + sys.path[0] + r"\ts\%s.ts -threads 2 "%name + sys.path[0] + "\mp4\%s.mp4"%name)
names = os.listdir("./mp4")
print(names)
fp = open('./list.txt','w')
for i in names:
fp.write("file 'mp4/%s'\n"%i)
fp.close()
end_time = time.time()
print(' Download complete , Total time :',end_time-start_time)
os.system('pause')

Merge mp4.bat

ffmpeg-new -f concat -i list.txt -c copy out.mp4
pause

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved