python將ts轉(zhuǎn)換成MP4

代碼可以直接運(yùn)行。

# -*- coding: utf-8 -*-

from urllibimport request

import os

from multiprocessingimport Pool

from globimport glob

def run(i):

url ="https://hong.tianzhen-zuida.com/20191220/16480_107655d7/1000k/hls/3564346f72c00000"+str(i)+".ts"

? ? print("開始下載:"+url)

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"}

r = request.Request(url, headers = headers)

# print(r.content)

? ? try:

downloaded_file =open('./mp4/{}'.format(url[-10:]),'wb')

req = request.Request(url, headers={'User-Agent':'Mozilla/5.0'})

file_on_web = request.urlopen(req)

print("downloading...")

while True:

buf = file_on_web.read(65536)

if len(buf) ==0:

break

? ? ? ? ? ? downloaded_file.write(buf)

downloaded_file.close()

file_on_web.close()

print("done")

except request.ContentTooShortError:

print('Network conditions is not good.Reloading.')

else:

return ""

# 對(duì)轉(zhuǎn)換的TS文件進(jìn)行排序

def get_sorted_ts(user_path):

ts_list = glob(os.path.join(user_path, '*.ts'))

boxer = []

for tsin ts_list:

if os.path.exists(ts):

# print(os.path.splitext(os.path.basename(ts)))

? ? ? ? ? ? file, _ = os.path.splitext(os.path.basename(ts))

print(int(file.replace("c", "")))

boxer.append(int(int(file.replace("c", ""))))

boxer.sort()

print(boxer)

return boxer

# 文件合并

def convert_m3u8(boxer, o_file_name):

# cmd_arg = str(ts0)+"+"+str(ts1)+" "+o_file_name

? ? tmp = []

for tsin boxer:

print(ts)

tmp.append("mp4\\c00000" +str(ts) +'.ts')

cmd_str ='+'.join(tmp)

exec_str ="copy /b " + cmd_str +' ' +" mp4\\" + o_file_name

print(exec_str)

res = os.popen(exec_str)

print(res.read())

if __name__ =='__main__':

# 創(chuàng)建進(jìn)程池,執(zhí)行10個(gè)任務(wù)

? ? print("開始執(zhí)行")

pool = Pool(10)

for iin range(10):

pool.apply_async(run, (i,))#執(zhí)行任務(wù)

? ? pool.close()

pool.join()

print('下載完成!')

user_path ="E:\\code\\Python\\mp4\\"

? ? o_file_name ="慶余年第一季(2019)第46集.mp4"

? ? boxer = get_sorted_ts(user_path)

convert_m3u8(boxer,o_file_name)

os.system('del /Q mp4\\*.ts')

print("刪除文件")

print('ok!處理完成')

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,918評(píng)論 0 13
  • =========================================================...
    lavor閱讀 3,649評(píng)論 0 5
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,335評(píng)論 0 17
  • 六月的海風(fēng)在輕拂夏天,也在輕拂我的臉龐,那淡淡的咸味,是思念的味道。 寂寞已成海。我獨(dú)自在這汪洋大海中前行,夢(mèng)...
    大海無量丷閱讀 701評(píng)論 0 0
  • 最近的刺繡作品是一幅巨無霸仙人掌。 繡藝一般,大家見笑了。
    A曦荷閱讀 353評(píng)論 0 4

友情鏈接更多精彩內(nèi)容