最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)
    # 用來(lái)保存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 Multiprocessing多進(jìn)程 使用tqdm顯示進(jìn)度條的實(shí)現(xiàn)

    Python Multiprocessing多進(jìn)程 使用tqdm顯示進(jìn)度條的實(shí)現(xiàn)

    這篇文章主要介紹了Python Multiprocessing多進(jìn)程 使用tqdm顯示進(jìn)度條的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 利用python-pypcap抓取帶VLAN標(biāo)簽的數(shù)據(jù)包方法

    利用python-pypcap抓取帶VLAN標(biāo)簽的數(shù)據(jù)包方法

    今天小編就為大家分享一篇利用python-pypcap抓取帶VLAN標(biāo)簽的數(shù)據(jù)包方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python 為什么說(shuō)eval要慎用

    python 為什么說(shuō)eval要慎用

    這篇文章主要介紹了python 為什么說(shuō)eval要慎用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python字符串逐字符或逐詞反轉(zhuǎn)方法

    Python字符串逐字符或逐詞反轉(zhuǎn)方法

    這篇文章主要介紹了Python字符串逐字符或逐詞反轉(zhuǎn)方法,本文對(duì)逐字符或逐詞分別給出兩種方法,需要的朋友可以參考下
    2015-05-05
  • 一步步教你用python的scrapy編寫一個(gè)爬蟲

    一步步教你用python的scrapy編寫一個(gè)爬蟲

    這篇文章主要給大家介紹了如何利用python的scrapy編寫一個(gè)爬蟲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用scrapy具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • python實(shí)現(xiàn)微信打飛機(jī)游戲

    python實(shí)現(xiàn)微信打飛機(jī)游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信打飛機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 深入探究Django中的Session與Cookie

    深入探究Django中的Session與Cookie

    周末小閑在家研究了下django的 cookie 和 session,所以下面這篇文章主要給大家深入地介紹了關(guān)于Django中Session與Cookie的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面跟著小編來(lái)一起看看吧。
    2017-07-07
  • windows中python實(shí)現(xiàn)自動(dòng)化部署

    windows中python實(shí)現(xiàn)自動(dòng)化部署

    本文主要介紹了windows中python實(shí)現(xiàn)自動(dòng)化部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 在Pytorch中計(jì)算卷積方法的區(qū)別詳解(conv2d的區(qū)別)

    在Pytorch中計(jì)算卷積方法的區(qū)別詳解(conv2d的區(qū)別)

    今天小編就為大家分享一篇在Pytorch中計(jì)算卷積方法的區(qū)別詳解(conv2d的區(qū)別),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python中os庫(kù)的函數(shù)使用

    python中os庫(kù)的函數(shù)使用

    這篇文章主要介紹了python中os庫(kù)的使用,本篇文章記錄下python中os庫(kù)的一些函數(shù)使用,對(duì)python?os庫(kù)使用感興趣的朋友跟隨小編一起看看吧
    2022-10-10

最新評(píng)論

五家渠市| 新干县| 漳浦县| 长阳| 浦北县| 改则县| 镇赉县| 明光市| 富顺县| 梁河县| 楚雄市| 盈江县| 华容县| 英德市| 高青县| 定襄县| 临泽县| 崇左市| 竹溪县| 鸡泽县| 丹棱县| 西乌珠穆沁旗| 泌阳县| 休宁县| 中西区| 衡南县| 扶余县| 玉溪市| 汶川县| 松阳县| 康马县| 嵊泗县| 水富县| 阳曲县| 新邵县| 铜川市| 龙陵县| 南投县| 黑龙江省| 永福县| 乌拉特中旗|