Last active
August 29, 2015 13:57
-
-
Save ranlix/9623485 to your computer and use it in GitHub Desktop.
将包含一个DRM视频文件转换为live流(播放列表无限循环)
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 -*- | |
''' | |
Created on 2013-4-23 | |
@author: xweiyan | |
@modify by Ran | |
version 2.0 | |
main change: | |
1.add counter | |
2.sequence automatic increase | |
3.playlist thread generate | |
''' | |
from hls import load | |
import hls | |
import os | |
import re | |
import sys | |
import shutil | |
import string | |
import tempfile | |
import time | |
import threading | |
''' | |
SCAN the segments folder | |
''' | |
''' | |
scan the segments folder, return a list | |
''' | |
''' | |
counter | |
''' | |
path = sys.argv[1] | |
class Counter: | |
def __init__(self): | |
self.lock = threading.Lock() | |
self.value = 0 | |
def increment(self): | |
self.lock.acquire() # critical section | |
self.value = value = self.value + 1 | |
self.lock.release() | |
return value | |
counter = Counter() | |
def scan(path): | |
_segments = {} | |
files = os.listdir(path) | |
files.sort() | |
for file in files: | |
if os.path.splitext(file)[1] == ".ts": | |
_segments[string.atoi(os.path.splitext(file)[0].split('-')[2])] = 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 txt2dict(file): | |
segments = {} | |
content = open(file).read().strip() | |
data = hls.M3U8(content) | |
for no, segment in enumerate(data.segments): | |
segments[no] = str(segment) | |
return segments | |
def string_to_lines(string): | |
return string.strip().replace('\r\n', '\n').split('\n') | |
''' | |
#EXTM3U | |
#EXT-X-VERSION:3 | |
#EXT-X-TARGETDURATION:11 | |
#EXT-X-MEDIA-SEQUENCE:56 | |
#EXTINF:10, | |
20130221T134839-01-56.ts | |
#EXTINF:10, | |
20130221T134839-01-57.ts | |
#EXTINF:10, | |
20130221T134839-01-58.ts | |
#EXTINF:10, | |
20130221T134839-01-59.ts | |
#EXTINF:10, | |
20130221T134839-01-60.ts | |
#EXTINF:10, | |
20130221T134839-01-61.ts | |
''' | |
def playlist(pl_uri, segments, index, keys): | |
#get the last playlist sequence index | |
_segments = load(pl_uri).segments | |
m = re.search("(\w*)-(\d+)-(\d+).ts", str(_segments)) | |
sequence = int(m.group(3)) | |
playList = [] | |
valueList = segments.values() | |
if sequence + 6 <= len(segments) - 1: | |
playList = valueList[sequence + 1: sequence + 7] | |
else: | |
playList = valueList[sequence + 1:] + valueList[:7 - len(segments) + sequence] | |
output = ['#EXTM3U', '#EXT-X-VERSION:3', '#EXT-X-TARGETDURATION:7'] | |
output.append("#EXT-X-MEDIA-SEQUENCE:" + str(index)) | |
for segment in playList: | |
# output.append(segment) | |
segment_no = int(segment[-6:-3]) | |
key_num = segment_no / 10 | |
output.append(keys[key_num]) | |
output.append(segment) | |
return '\n'.join(output) | |
class Generate(threading.Thread): | |
def __init__(self, path, interval=6): | |
threading.Thread.__init__(self) | |
self.path = path | |
self.interval = interval | |
self.lock = threading.Lock() | |
self.files = [] | |
for file in os.listdir(path): | |
if os.path.splitext(file)[1] == ".txt": | |
self.files.append(file) | |
def run(self): | |
while True: | |
self.lock.acquire() | |
index = counter.increment() | |
for file in self.files: | |
splititem = os.path.splitext(file) | |
m3u8 = os.path.join(self.path, splititem[0] + ".m3u8") | |
keys = txt2keyList(os.path.join(path, file)) | |
segments = txt2dict(os.path.join(self.path, file)) | |
# print segments | |
#create a tempple file | |
temp = tempfile.mktemp() | |
# print temp | |
file = open(temp, 'w') | |
output = playlist(m3u8, segments, index, keys) | |
# print output | |
file.write(output) | |
file.close() | |
shutil.copy(temp, m3u8) | |
try: | |
os.remove(temp) | |
except OSError: | |
pass | |
time.sleep(self.interval) | |
self.lock.release() | |
if __name__ == "__main__": | |
gen = Generate(path) | |
gen.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment