欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 下載m3u8視頻的示例代碼

 更新時(shí)間:2020年11月11日 16:01:06   作者:凹凸曼大人  
這篇文章主要介紹了python 下載m3u8視頻的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
import requests
import os
import datetime
import threading

class xiazai():
  def __init__(self,url):
    self.url = url
    work_dir = os.getcwd()
    # print(work_dir)
    # 用來保存ts文件
    file_dir = os.path.join(work_dir, 'file_tmp')
    if not os.path.exists(file_dir):
      os.mkdir(file_dir)
    self.headers ={
      'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
    }
    self.savefile(self.url)

  def savefile(self, file_url):

    r = requests.get(file_url, headers=self.headers)
    # 合成帶有hls的m3u8地址
    if r.text.split('\n')[-1] == '':
      hls_mark = r.text.split('\n')[-2] # 以防\n結(jié)尾
    else:
      hls_mark = r.text.split('\n')[-1]
    self.url_m3u8_hls = file_url.replace('index.m3u8', hls_mark)
    #file_m3u8 = url_m3u8_hls.split('/')[-1]
    self.duqu()
    #print(url_m3u8_hls)

  def duqu(self):
    r = requests.get(self.url_m3u8_hls, headers=self.headers).text
    text_bytes = r.split('\n')
    # 篩選以.ts結(jié)尾的行
    # 有些情況下可能是以其他格式的文件,比如png,下載后修改后綴即可
    # ts_name = [i for i in text_string if i.endswith('.ts')]
    self.ts_time = [i for i in text_bytes if i.startswith('#EXTINF')]
    #self.shijian(dm_time)
    #print(dm_time)
    self.ts_neirong = [i for i in text_bytes if not i.startswith('#')]
    self.ts_neirong.pop()
    self.threads = []
    self.threads.append(threading.Thread(target=self.xiazai))
    self.threads.append(threading.Thread(target=self.shijian))
    for t in self.threads:
      # print(t)
      t.start()
    #self.xiazai(url_m3u8_hls)
    # print(ts_neirong)
  def shijian(self):
    self.dm_time = 0
    for i in range(len(self.ts_time)):
      ts_time1 = self.ts_time[i].replace('#EXTINF:', '')
      ts_time2 = ts_time1.replace(',', '')
      self.dm_time = float(ts_time2) + self.dm_time
    shichang_time = str(datetime.timedelta(seconds=self.dm_time))
    print('視頻時(shí)長(zhǎng):%s' % shichang_time)
  def xiazai(self):
    liebiao=[]
    for i in range(len(self.ts_neirong)):
      hls_mark = self.url_m3u8_hls.split('/')[-1]
      url_xiazai = self.url_m3u8_hls.replace(hls_mark, self.ts_neirong[i])
      liebiao.append(url_xiazai)
      #print(url_xiazai)
      # r = requests.get(url_xiazai, headers=self.headers)
      # with open('file_tmp/'+ts_neirong[i], 'wb') as f:
      # f.write(r.content)
      # f.close()
    x = self.bisector_list(liebiao, 10)
    self.xiancheng0=x[0]
    self.xiancheng1=x[1]
    self.xiancheng2=x[2]
    self.xiancheng3=x[3]
    self.xiancheng4=x[4]
    self.xiancheng5=x[5]
    self.xiancheng6=x[6]
    self.xiancheng7=x[7]
    self.xiancheng8=x[8]
    self.xiancheng9=x[9]
    self.threads2 = []
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai1))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai2))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai3))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai4))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai5))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai6))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai7))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai8))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai9))
    self.threads2.append(threading.Thread(target=self.xiancheng_xiazai10))

    for t in self.threads2:
      # print(t)
      t.start()


  def xiancheng_xiazai1(self):
    #print(self.xiancheng0)
    for i in self.xiancheng0:
      #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()

  def xiancheng_xiazai2(self):
    #print(self.xiancheng1)
    for i in self.xiancheng1:
      #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi= i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()

  def xiancheng_xiazai3(self):
    #print(self.xiancheng2)
    for i in self.xiancheng2:
      #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()

  def xiancheng_xiazai4(self):
    #print(self.xiancheng3)
    for i in self.xiancheng3:
      #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def xiancheng_xiazai5(self):
    #print(self.xiancheng4)
    for i in self.xiancheng4:
    #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def xiancheng_xiazai6(self):
    #print(self.xiancheng5)
    for i in self.xiancheng5:
    #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def xiancheng_xiazai7(self):
    #print(self.xiancheng6)
    for i in self.xiancheng6:
    #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def xiancheng_xiazai8(self):
    #print(self.xiancheng7)
    for i in self.xiancheng7:
    #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def xiancheng_xiazai9(self):
    #print(self.xiancheng8)
    for i in self.xiancheng8:
    #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def xiancheng_xiazai10(self):
    #print(self.xiancheng9)
    for i in self.xiancheng9:

    #print(i)
      r = requests.get(i, headers=self.headers)
      mingzi = i.split('/')[-1]
      with open('file_tmp/'+mingzi, 'wb') as f:
        f.write(r.content)
    f.close()


  def bisector_list(self,tabulation: list, num: int):
    """
    將列表平均分成幾份
    :param tabulation: 列表
    :param num: 份數(shù)
    :return: 返回一個(gè)新的列表
    """
    new_list = []

    '''列表長(zhǎng)度大于等于份數(shù)'''
    if len(tabulation) >= num:
      '''remainder:列表長(zhǎng)度除以份數(shù),取余'''
      remainder = len(tabulation) % num
      if remainder == 0:
        '''merchant:列表長(zhǎng)度除以分?jǐn)?shù)'''
        merchant = int(len(tabulation) / num)
        '''將列表平均拆分'''
        for i in range(1, num + 1):
          if i == 1:
            new_list.append(tabulation[:merchant])
          else:
            new_list.append(tabulation[(i - 1) * merchant:i * merchant])
        return new_list
      else:
        '''merchant:列表長(zhǎng)度除以分?jǐn)?shù) 取商'''
        merchant = int(len(tabulation) // num)
        '''remainder:列表長(zhǎng)度除以份數(shù),取余'''
        remainder = int(len(tabulation) % num)
        '''將列表平均拆分'''
        for i in range(1, num + 1):
          if i == 1:
            new_list.append(tabulation[:merchant])
          else:
            new_list.append(tabulation[(i - 1) * merchant:i * merchant])
            '''將剩余數(shù)據(jù)的添加前面列表中'''
            if int(len(tabulation) - i * merchant) <= merchant:
              for j in tabulation[-remainder:]:
                new_list[tabulation[-remainder:].index(j)].append(j)
        return new_list
    else:
      '''如果列表長(zhǎng)度小于份數(shù)'''
      for i in range(1, len(tabulation) + 1):
        tabulation_subset = []
        tabulation_subset.append(tabulation[i - 1])
        new_list.append(tabulation_subset)
      return new_list

if __name__=='__main__':
  xiazai('http://iqiyi.cdn9-okzy.com/20200907/15137_ed25d8c5/index.m3u8')

速度很慢.  40m 5分鐘  不加多線程 1小時(shí)。 可能我的m3u8不行,或者我電腦不行, 多線程是機(jī)械式的。  電腦好 可以多加幾條。

以上就是python 下載m3u8視頻的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 下載m3u8視頻的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名

    python實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名

    這篇文章主要介紹了python實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名,主要以python怎么快速對(duì)請(qǐng)求體做一次簽名為主題,塑造實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)包簽名過程,具有一定得參考價(jià)值,需要的小伙伴可以參考一下
    2022-02-02
  • Python字典遍歷操作實(shí)例小結(jié)

    Python字典遍歷操作實(shí)例小結(jié)

    這篇文章主要介紹了Python字典遍歷操作,結(jié)合實(shí)例形式總結(jié)分析了Python遍歷字典鍵值對(duì)、遍歷鍵、遍歷值等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python MNIST手寫體識(shí)別詳解與試練

    Python MNIST手寫體識(shí)別詳解與試練

    MNIST(官方網(wǎng)站)是非常有名的手寫體數(shù)字識(shí)別數(shù)據(jù)集,在Tensorflow的官方網(wǎng)站里,第一個(gè)就拿它來做實(shí)戰(zhàn)講解,咱們也以此作為開始的項(xiàng)目
    2021-11-11
  • Python實(shí)現(xiàn)提取語句中的人名

    Python實(shí)現(xiàn)提取語句中的人名

    這篇文章主要為大家介紹一個(gè)小工具:可以將語句中的人名提取出來。文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-01-01
  • pandas去除重復(fù)值的實(shí)戰(zhàn)

    pandas去除重復(fù)值的實(shí)戰(zhàn)

    本文主要介紹了pandas去除重復(fù)值的實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Python中集合的內(nèi)置函數(shù)詳解

    Python中集合的內(nèi)置函數(shù)詳解

    這篇文章主要介紹了Python中集合的內(nèi)置函數(shù)詳解,集合在python里面是另外一種對(duì)象,它的特征是在大括號(hào)里面,并且各個(gè)元素是唯一的,今天我們來看一下其內(nèi)置了哪些函數(shù),需要的朋友可以參考下
    2023-08-08
  • Python程序退出方式小結(jié)

    Python程序退出方式小結(jié)

    這篇文章主要介紹了Python程序退出方式小結(jié),具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Python清理數(shù)據(jù)的八種常用方法

    Python清理數(shù)據(jù)的八種常用方法

    在本文中,我們將介紹Python中一些常用的數(shù)據(jù)清理方法,包括數(shù)據(jù)預(yù)覽、缺失值處理、異常值處理、數(shù)據(jù)類型轉(zhuǎn)換、重復(fù)值處理、數(shù)據(jù)標(biāo)準(zhǔn)化、特征選擇以及處理類別數(shù)據(jù),感興趣的可以了解一下
    2023-11-11
  • Python 中將二進(jìn)制轉(zhuǎn)換為整數(shù)的多種方法

    Python 中將二進(jìn)制轉(zhuǎn)換為整數(shù)的多種方法

    這篇文章主要介紹了Python 中將二進(jìn)制轉(zhuǎn)換為整數(shù),Python 中提供了多種方式將二進(jìn)制字符串轉(zhuǎn)換為整數(shù),其中包括使用 int() 函數(shù)、使用二進(jìn)制前綴和使用 eval() 函數(shù),本文通過實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • python lxml中etree的簡(jiǎn)單應(yīng)用

    python lxml中etree的簡(jiǎn)單應(yīng)用

    這篇文章主要介紹了python lxml中etree的簡(jiǎn)單應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評(píng)論