Last active
August 29, 2015 13:57
-
-
Save ranlix/9579636 to your computer and use it in GitHub Desktop.
当前的nts脚本无法满足内网所需现有的DRM streaming转化成nts流的需求,因为key的结构不一样,所以在原有的脚本基础上添加了提取key的函数(txt2keyList),更新将key写入m3u8文件的算法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import os | |
import sys | |
import shutil | |
import string | |
import tempfile | |
import time | |
from hls import load, parser | |
import hls | |
path = sys.argv[1] | |
def scan(path): | |
"""scan(path), return a dictionary | |
{ | |
0: 'air-race-02-00000.ts', | |
1: 'air-race-02-00001.ts', | |
... | |
29: 'air-race-02-00029.ts'} | |
""" | |
_segments = {} | |
files = os.listdir(path) | |
files.sort() | |
for file in files: | |
if os.path.splitext(file)[1] == ".ts": | |
filekey = string.atoi(os.path.splitext(file)[0].split('-')[3]) | |
_segments[filekey] = file | |
return _segments | |
def txt2keyList(txt): | |
keylist = [] | |
for i in open(txt).read().split(): | |
if i.startswith("#EXT-X-KEY"): | |
keylist.append(i) | |
# keylist.sort() | |
return keylist | |
# def add_before_certain(alist, certain, item): | |
# """Add items into the list and before a certain element one by one""" | |
# add_position = alist.index(certain) | |
# alist.insert(add_position, item) | |
# return alist | |
def txt2dict(file): | |
""" | |
m3u8 = r"F:\Dropbox\Ran\drmTest\air-race-02-nodrm.m3u8" | |
txt2dict(m3u8): | |
{ | |
0: '#EXTINF:6.011911,\nair-race-02-00000.ts', | |
1: '#EXTINF:6.002656,\nair-race-02-00001.ts', | |
2: '#EXTINF:6.039844,\nair-race-02-00002.ts', | |
3: '#EXTINF:6.030589,\nair-race-02-00003.ts'} | |
""" | |
segments = {} | |
content = open(file).read().strip() | |
data = hls.M3U8(content) | |
for no, segment in enumerate(data.segments): | |
# print "no: ", no | |
# print "segments: ", segments | |
segments[no] = str(segment) | |
return segments | |
def string_to_lines(string): | |
return string.strip().replace('\r\n', '\n').split('\n') | |
def playlist(pl_uri, segments, n, keys): | |
playList = [] | |
playList = list(segments.values())[: n] | |
output = ['#EXTM3U', '#EXT-X-VERSION:3', '#EXT-X-TARGETDURATION:7'] | |
output.append("#EXT-X-MEDIA-SEQUENCE:" + "0") | |
for segment in playList: | |
segment_no = int(segment[-6:-3]) | |
key_num = segment_no / 10 | |
if not (segment_no % 10): | |
output.append(keys[key_num]) | |
output.append(segment) | |
if n == len(segments): | |
output.append("#EXT-X-ENDLIST") | |
return '\n'.join(output) | |
def generate(path, interval=6): | |
files = [] | |
# n = 6 | |
flag = 0 | |
nums = {} | |
for file in os.listdir(path): | |
if os.path.splitext(file)[1] == ".txt": | |
files.append(file) | |
nums[os.path.splitext(file)[0]] = 6 | |
while True: | |
for file in files: | |
m3u8 = os.path.splitext(file)[0] + ".m3u8" | |
m3u8 = os.path.join(path, m3u8) | |
keys = txt2keyList(os.path.join(path, file)) | |
segments = txt2dict(os.path.join(path, file)) | |
n = os.path.splitext(file)[0] | |
#create a tempple file | |
temp = tempfile.mktemp() | |
file = open(temp, 'w') | |
if nums[n] == len(segments) + 1: | |
nums[n] = 6 | |
flag = 1 | |
else: | |
output = playlist(m3u8, segments, nums[n], keys) | |
file.write(output) | |
file.close() | |
shutil.copy(temp, m3u8) | |
try: | |
os.remove(temp) | |
except OSError: | |
pass | |
nums[n] = nums[n] + 1 | |
if flag == 1: | |
time.sleep(120) | |
flag = 0 | |
time.sleep(interval) | |
if __name__ == "__main__": | |
generate(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment