20230506在Ubuntu22.04下使用python3下载合并ts切片

20230506在Ubuntu22.04下使用python3下载合并ts切片
2023/5/6 19:42

本文主要是和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)


三、
输出合并ts切片的shell脚本:
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$ 

在WIN7/WIN10下都可以下载红杏林/高手知路(猫抓获取链接),但是直接移植到Ubuntu22.04下异常!
后来确认读取的line行是字符串类型,但是在Ubuntu下读取行的时候会将最后的换行符也读取进来了!
干掉换行符就正常了!
【一步一步地调试,搞了了2小时!】


参考资料;
python read line signature not match
python line = f.readline()
python line = f.readline() line转字符串
python去掉字符串最后的换行符


python str 长度
https://blog.csdn.net/weixin_39661353/article/details/110551584
python想获取字符串str的长度_在python中如何获取字符串的长度


python 判断字符串是否 相等
https://blog.csdn.net/update7/article/details/129718382
Python字符串比较:如何判断两个字符串是否相等?


python 变量类型判断
https://blog.csdn.net/qq_39187019/article/details/109256713
Python判断变量的类型


https://blog.csdn.net/weixin_31866177/article/details/115121857
【Python】判断变量类型(是否为int、字符串、列表、元组、字典等方法)


https://blog.csdn.net/weixin_42930696/article/details/123060083
python中判断变量的类型


https://www.zhihu.com/question/592560444/answer/2961251162?utm_id=0
在 Python 中,如何判断一个变量的类型?


https://blog.csdn.net/weixin_39887183/article/details/113981247
python循环控制语句将数值转化成字符串_python基础入门详解(文件输入/输出内建类型字典操作使用方法)...


https://blog.csdn.net/weixin_35662493/article/details/112033112
python用readline读取数据库_PYTHON学习14.06:Python readline()和readlines()函数:按行读取文件...

猜你喜欢

转载自blog.csdn.net/wb4916/article/details/130534556