使用python 下載m3u8格式視訊,併合成
-- coding: utf-8 --
Created on 2018/07/26
import os import requests
""" 下載M3U8檔案裡的所有片段 """
def download(url): download_path = os.getcwd() + "/download" if not os.path.exists(download_path): os.mkdir(download_path,0777) all_content = requests.get(url).text # 獲取M3U8的檔案內容 file_line = all_content.split("\r\n") # 讀取檔案裡的每一行 # 通過判斷檔案頭來確定是否是M3U8檔案 if file_line[0] != "#EXTM3U": raise BaseException(u"非M3U8的連結") else: unknow = True # 用來判斷是否找到了下載的地址 for index, line in enumerate(file_line): if "EXTINF" in line: unknow = False # 拼出ts片段的URL pd_url = url.rsplit("/", 1)[0] + "/" + file_line[index + 1] print pd_url res = requests.get(pd_url) c_fule_name = str(file_line[index + 1]) with open(download_path + "/" + c_fule_name, 'ab') as f: f.write(res.content) f.flush() if unknow: raise BaseException("未找到對應的下載連結") else: print u"下載完成"
if name == 'main': download("http://hzresource.cdn.bcebos.com/hls/geruikldsl/hls.m3u8")
使用shell 指令碼 把下載的ts檔案追加到一個檔案中
root@alex-virtual-machine:/www/movie/v# for i in ls *.ts
; do cat $i >> com.ts; done
root@alex-virtual-machine:/www/movie/v# ls com.ts out007.ts out015.ts out023.ts out031.ts out039.ts out047.ts out055.ts out063.ts out071.ts out079.ts out087.ts out095.ts out103.ts out111.ts out119.ts out127.ts out135.ts out000.ts out008.ts out016.ts out024.ts out032.ts out040.ts out048.ts out056.ts out064.ts out072.ts out080.ts out088.ts out096.ts out104.ts out112.ts out120.ts out128.ts out136.ts out001.ts out009.ts out017.ts out025.ts out033.ts out041.ts out049.ts out057.ts out065.ts out073.ts out081.ts out089.ts out097.ts out105.ts out113.ts out121.ts out129.ts out137.ts out002.ts out010.ts out018.ts out026.ts out034.ts out042.ts out050.ts out058.ts out066.ts out074.ts out082.ts out090.ts out098.ts out106.ts out114.ts out122.ts out130.ts out138.ts out003.ts out011.ts out019.ts out027.ts out035.ts out043.ts out051.ts out059.ts out067.ts out075.ts out083.ts out091.ts out099.ts out107.ts out115.ts out123.ts out131.ts out139.ts out004.ts out012.ts out020.ts out028.ts out036.ts out044.ts out052.ts out060.ts out068.ts out076.ts out084.ts out092.ts out100.ts out108.ts out116.ts out124.ts out132.ts out140.ts out005.ts out013.ts out021.ts out029.ts out037.ts out045.ts out053.ts out061.ts out069.ts out077.ts out085.ts out093.ts out101.ts out109.ts out117.ts out125.ts out133.ts out.m3u8 out006.ts out014.ts out022.ts out030.ts out038.ts out046.ts out054.ts out062.ts out070.ts out078.ts out086.ts out094.ts out102.ts out110.ts out118.ts out126.ts out134.ts
把合成後的ts檔案轉換成mp4檔案即可
root@alex-virtual-machine:/www/movie/v# ffmpeg -i com.ts -acodec copy -vcodec copy -bsf aac_adtstoasc output.mp4
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, mpegts, from 'com.ts':
Duration: 00:23:21.27, start: 1.400000, bitrate: 265 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709), 1366x768 [SAR 1:1 DAR 683:384], 15 fps, 15 tbr, 90k tbn, 30 tbc
Stream #0:10x101: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 58 kb/s
[mp4 @ 0x1c52600] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 0x1c52600] Codec for stream 1 does not use global headers but container format requires global headers
Output #0, mp4, to 'output.mp4':
Metadata:
encoder : Lavf56.40.101
Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1366x768 [SAR 1:1 DAR 683:384], q=2-31, 15 fps, 15 tbr, 90k tbn, 90k tbc
Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, 58 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=21019 fps=0.0 q=-1.0 Lsize= 40433kB time=00:23:21.30 bitrate= 236.4kbits/s
video:30668kB audio:9136kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.579515%
閱讀原文請訪問
www.zxb8.cc