20230506 Use python3 to download and merge ts slices under Ubuntu22.04

20230506 Use python3 to download and merge ts slices under Ubuntu22.04
2023/5/6 19:42

This article is mainly to compare the differences with WIN7/WIN10!


一、
Z:\1575\buquan-ts1574.py
import requests
from multiprocessing import Pool

def mission(url,n):
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
    print('*****')
    response=requests.get(url,headers=headers)
    print('-----')
    #f=open("./1/%03d.ts"%n,"wb")
    #f=open("./1574/%03d.ts"%n,"wb")
    f=open("1574/%03d.ts"%(n+1),"wb")
    f.write(response.content)      
    f.close()
    print("%03d.ts OK..."%n)


if __name__ == "__main__":
    pool=Pool(20)
    #for n in range(0,1000):
    #for n in range(0,2000):
    #for n in range(0,3000):
    #for n in range(0,4000):
    for n in range(0,6000):
        #https://c-vod.hw-cdn.xiaoeknow.com/2919df88vodtranscq1252524126/4b2eee3d387702306097169767/v.f1231726_0.ts?t=632730dc&sign=a3481f748245b4d8a8aa839740614c02&us=OKmcBFQEBk
        url = "https://c-vod.hw-cdn.xiaoeknow.com/2919df88vodtranscq1252524126/9f53fa42243791581971767817/v.f1228559_{}.ts?t=1204d85ae&sign=f411640c263f18cbffe27a885bf5b8a6&us=HSrVYxtpip".format(n)
        print(url)
        pool.apply_async(mission,(url,n))
        
        #line = f.readline()
        #pool.apply_async(mission,(line,n))
    pool.close()
    pool.join()

 


二、
Z:\1575\ts1574.py
import requests
from multiprocessing import Pool

def mission(url,n):
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
    print('*****')
    response=requests.get(url,headers=headers)
    print('-----')
    #f=open("./1/%03d.ts"%n,"wb")
    f=open("./1574/%03d.ts"%n,"wb")
    f.write(response.content)      
    f.close()
    print("%03d.ts OK..."%n)


if __name__ == "__main__":
    #f = open("f:\\p1\\1574.txt")
    #f = open("g:\\1575\\1574.txt")
    f = open("1574.txt")
    
    pool=Pool(20)  
    #for n in range(1,1574):
    #for n in range(1,2000):
    for n in range(1,4000):
        line = f.readline()
        #pool.apply_async(mission,(line,n))
        pool.apply_async(mission,(line.rstrip(), n))
    pool.close()
    pool.join()
    
    print('\n 2023/5/6 1:46-finish')
    print(n)

 


3.
Output the shell script for merging ts slices:
temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(1,999):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%03d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output1.ts"
url = url + temp

f=open("1574/1.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(1000,2000):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output2.ts"
url = url + temp

f=open("1574/2.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(2001,3000):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output3.ts"
url = url + temp

f=open("1574/3.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
#for n in range(3001,3389):
for n in range(3001,4000):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output4.ts"
url = url + temp

f=open("1574/4.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(4001,5000):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output5.ts"
url = url + temp

f=open("1574/5.sh","w")
f.write(url)
f.close()

temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(5001,6000):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output6.ts"
url = url + temp

f=open("1574/6.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(6001,7000):
    #temp = "1574/%04d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output7.ts"
url = url + temp

f=open("1574/7.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(7001,8000):
    #temp = "1574/%04d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output8.ts"
url = url + temp

f=open("1574/8.sh","w")
f.write(url)
f.close()

temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(8001,9000):
    #temp = "1574/%04d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output9.ts"
url = url + temp

f=open("1574/9.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(9001,10000):
    #temp = "1574/%03d.ts"%n
    temp = "%04d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output10.ts"
url = url + temp

f=open("1574/10.sh","w")
f.write(url)
f.close()


temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(10001,11000):
    #temp = "1574/%03d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%04d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output11.ts"
url = url + temp

f=open("1574/11.sh","w")
f.write(url)
f.close()

temp = ""
url = "ffmpeg -i \"concat:"

#for n in range(1,10):
for n in range(11001,12000):
    #temp = "1574/%05d.ts"%n
    temp = "%03d.ts|"%n
    url = url + temp
    #print(temp)
    #print(url)

temp = "%05d.ts"%(n+1)
url = url + temp

    
temp = "\" -c copy output12.ts"
url = url + temp

f=open("1574/12.sh","w")
f.write(url)
f.close()

 


四、单独下载一个ts切片:
import requests
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
response=requests.get("https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi",headers=headers)
f=open("333.ts","wb")
f.write(response.content)      
f.close()


import requests
#from multiprocessing import Pool

#def mission(url,n):
#    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
#    print(headers)
#    print('*****')
#    response=requests.get(url,headers=headers)
#    print(url)
#    print(response)
#    print('-----')
#    #f=open("./1574/%03d.ts"%n,"wb")
#    #f=open("1574/%03d.ts"%n,"wb")
#    #f=open("%03d.ts"%n,"wb")
#    f=open("3332.ts","wb")
#    temp = f.write(response.content)
#    print(temp)
#    f.close()
#    print("%03d.ts OK..."%n)


if __name__ == "__main__":

    str1 = "hello"
    str2 = "hello"
    if str1 == str2:
        print("vvvv")
    else:
        print("xxxx")


    #pool=Pool(20)
    #f = open("c:\\1575\\1574.txt")
    f = open("1574.txt")
    
    #headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
    #response=requests.get("https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi",headers=headers)
    #f=open("333.ts","wb")
    #f.write(response.content)      
    #f.close()
    
    for n in range(1,1575):
        url=""
        print(n)
        print(url)
        #pool.apply_async(mission,(url,n))
        #mission(url,n)
        
        line = f.readline()
        #if str1 == str2:
        if line == "https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi":
            print("v20230506")
        else:
            print("????")
        
        
        print(line)
        print(len(line))
        
        print(line.rstrip())
        print(len(line.rstrip()))
        
        print('-----')
        print("https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi")
        print(len("https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi"))
        #print(type(x))
        print(type(line))
        #mission(line,n)
        
        #line2[200] =  "xyb"
        #print(line2)
        char_array = ['a', 'b', 'c']
        print(char_array)
        
        headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
        #response=requests.get("https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi",headers=headers)
        #line[-1]=''
        print(line[0])
        print(line[len(line)-2])
        print(line[len(line)-1])
        #line[len(line)-1]=''
        #line[len(line)-2]=''
        #line[len(line)-1]='\0'
        
        #line2 = ''
        #
        #for i in range(0,len(line)-1):
        for i in range(0,len(line)):
            print(line[i])
            #line[i] = line2[i]
        
        #print(line2)
        #print(len(line2))
        
        #response=requests.get(line, headers=headers)
        response=requests.get(line.rstrip(), headers=headers)
        f=open("333.ts","wb")
        temp = f.write(response.content)
        print(temp)      
        f.close()
    #pool.close()
    #pool.join()

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/1575$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/1575$ python3 dandu.py 
vvvv
1

????
https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi

193
https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi
192
-----
https://btt-vod.xiaoeknow.com/522ff1e0vodcq1252524126/3ada4c57243791581932707725/playlist.f3_0.ts?start=0&end=1555323&type=mpegts&sign=af0b97077eab8cbd57032f45525f31af&t=6455ce40&us=kCHNVAFqLi
192
<class 'str'>
['a', 'b', 'c']
h
i


12
2

Traceback (most recent call last):
  File "/home/rootroot/1575/dandu.py", line 49, in <module>
    line = f.readline()
io.UnsupportedOperation: read
rootroot@rootroot-adol-ADOLBOOK-I421UAY -ADOL14UA:~/1575$ 

Under WIN7/WIN10, you can download Hongxinglin/Gaoshouzhilu (cat catch to get the link), but it is abnormal to directly transplant it to Ubuntu22.04!
Later, it was confirmed that the read line is a string type, but when reading the line under Ubuntu, the last newline character will also be read in!
Get rid of the line breaks and it's normal!
[Debugging step by step, it took 2 hours!


Reference materials;
python read line signature not match
python line = f.readline()
python line = f.readline() line to string
python removes the last newline character in the string


python str length
https://blog.csdn.net/weixin_39661353/article/details/110551584
python wants to get the length of the string str_ how to get the length of the string in python


Python judges whether strings are equal
https://blog.csdn.net/update7/article/details/129718382
Python string comparison: how to judge whether two strings are equal?


Python variable type judgment
https://blog.csdn.net/qq_39187019/article/details/109256713
Python judgment variable type


https://blog.csdn.net/weixin_31866177/article/details/115121857
[Python] Determine variable type (whether it is int, string, list, tuple, dictionary, etc.)


https://blog.csdn.net/weixin_42930696/article/details/123060083
judging the type of variable in python


https://www.zhihu.com/question/592560444/answer/2961251162?utm_id=0
How to judge the type of a variable in Python?


https://blog.csdn.net/weixin_39887183/article/details/113981247
python loop control statement converts values ​​into strings_detailed introduction to python basics (file input/output built-in type dictionary operation method)...


https://blog.csdn.net/weixin_35662493/article/details/112033112
python uses readline to read database_PYTHON learning 14.06: Python readline() and readlines() functions: read files by line...

Guess you like

Origin blog.csdn.net/wb4916/article/details/130534556