diff --git a/config.py b/config.py index ef06da9..cfd8354 100644 --- a/config.py +++ b/config.py @@ -61,7 +61,7 @@ class Jsondata: def create_floder(path): pathlist = list(os.path.split(path)) pathlist.pop() - flordpath = os.path.join(pathlist) + flordpath = '/'.join(pathlist) if not os.path.exists(flordpath): _create_floder(flordpath) @@ -69,11 +69,12 @@ def create_floder(path): def _create_floder(path): pathlist = list(os.path.split(path)) pathlist.pop() - flordpath = os.path.join(pathlist) + flordpath = '/'.join(pathlist) if not os.path.exists(flordpath): _create_floder(flordpath) - os.mkdir(flordpath) + if not os.path.exists(path): + os.mkdir(path) global_config = {} msw_queue = queue.Queue() diff --git a/plugins/ffmpeg.py b/plugins/ffmpeg.py index 7d9f1cc..b24a510 100644 --- a/plugins/ffmpeg.py +++ b/plugins/ffmpeg.py @@ -3,7 +3,7 @@ import copy import os from mswp import Datapack from forwarder import receive_queues, send_queue -from config import msw_queue +from config import msw_queue, _create_floder from config import dprint as print receive_queue = receive_queues[__name__] @@ -17,33 +17,126 @@ def main(): class Ffmpeg_controller: def __init__(self): self.ffmpeg_type = None - self.status = 'disable' - self.padding_to_convert = [] - self.already_in_convert = [] - self.finished_convert = [] + self.status = 0 + self.server = None + self.mainloop() def mainloop(self): + _create_floder('resources/ffmpeg_tmp') + _create_floder('resources/ffmpeg_finished') + while True: dp = receive_queue.get() - if dp.method == 'post' and dp.body == b'split': # config ffmpeg is server or client + if dp.method == 'post' and dp.body == b'start': # config ffmpeg is server or client ndp = dp.reply() ndp.body = 'Spliting file %s' % dp.head['filename'] ndp.body = ndp.body.encode() send_queue.put(ndp) - if not os.path.exists('resources/ffmpeg_tmp'): - os.mkdir('resources/ffmpeg_tmp') - cmd = 'ffmpeg -i ' + dp.head['filename'] + ' -c copy -f segment -segment_time 20 \ - -reset_timestamps 1 resources/' + '%d' + '.mp4' + -reset_timestamps 1 -y resources/ffmpeg_tmp/' + '%d' + '.mp4' os.system(cmd) + self.run_as_server() + elif dp.method == 'post' and dp.body == b'enable': # clinet mode + self.status = 1 + self.server = dp.head['server'] + self.conver_func() + + elif dp.method == 'get': + ndp = dp.reply() + ndp.method = 'post' + ndp.body = b'disable' + print('let %s disabled' % dp.head['id']) + + def run_as_server(self): + _padding_to_convert = os.listdir('resources/ffmpeg_tmp') + padding_to_convert = [] + for file in _padding_to_convert: + file = 'resources/ffmpeg_tmp/' + file + padding_to_convert.append(file) + already_in_convert = {} # flag: filename + finished_convert = [] # outputfilename + + while True: + dp = receive_queue.get() + + if dp.method == 'post' and dp.body == b'status': + result = '' + result += 'padding_to_convert ' + str(padding_to_convert) + '\n' + result += 'already_in_convert ' + str(already_in_convert) + '\n' + result += 'finished_convert ' + str(finished_convert) + ndp = dp.reply() + ndp.body = result.encode() + send_queue.put(ndp) + + elif dp.method == 'get': + if padding_to_convert: + filename = padding_to_convert.pop(0) + already_in_convert[dp.head['flag']] = filename + elif already_in_convert: + key, filename = get_one_from_dict(already_in_convert) + already_in_convert[dp.head['flag']] = filename + del(already_in_convert[key]) + else: + print('woring') + continue + + ndp = dp.reply() + ndp.method = 'file' + ndp.head['filename'] = filename + print('%s get %s to convert' % (dp.head['id'], filename), dp) + + send_queue.put(ndp) + + elif dp.method == 'file': + os.remove(already_in_convert[dp.head['flag']]) + del(already_in_convert[dp.head['flag']]) + finished_convert.append(dp.head['filename']) + if not padding_to_convert and not already_in_convert: + print('convert finished') + return + + + def conver_func(self): + while self.status: + self.send_request() + + dp = receive_queue.get() + if dp.method == 'post' and dp.body == b'disable': + self.status = 0 + + elif dp.method == 'file': + filename = dp.head['filename'] + output_filename = filename[:-4] + '.mkv' + output_filename = output_filename.replace('ffmpeg_tmp', 'ffmpeg_finished') + os.system('ffmpeg -i ' + filename + ' -c:a libopus -ab 64k \ + -c:v libx265 -s 1280x720 -y ' + output_filename) + + ndp = dp.reply() + ndp.head['filename'] = output_filename + ndp.method = 'file' + send_queue.put(ndp) + + + def send_request(self): + dp = Datapack(head={'from': __name__}) + dp.method = 'get' + dp.app = 'ffmpeg' + dp.head['to'] = self.server + + send_queue.put(dp) + + +def get_one_from_dict(d): + for key in d: + return key, d[key] thread = threading.Thread(target=main, args=(), daemon=True) thread.start() diff --git a/plugins/net.py b/plugins/net.py index b5ed293..0d2d5d2 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -370,11 +370,13 @@ class Connection: if dp.method == 'file': create_floder(dp.head['filename']) + create_floder('tmp/' + dp.head['filename']) if dp.method == 'file' and os.path.exists(dp.head['filename']): os.remove(dp.head['filename']) except Exception as e: print('Decode head failed %s: %s' % (type(e), str(e))) + print(self.buff) continue length = int(dp.head.get('length')) @@ -396,7 +398,7 @@ class Connection: f.write(self.buff[:still_need]) else: dp.body = self.buff[:still_need] - self.buff = self.buff[still_need:] + self.buff = self.buff[still_need:] still_need = 0 # bleow code are using to process datapack diff --git a/plugins/update.py b/plugins/update.py index ee74c5d..8dfdecd 100644 --- a/plugins/update.py +++ b/plugins/update.py @@ -8,7 +8,7 @@ receive_queue = receive_queues[__name__] remove_file_list = ['__init__.py', 'addrlist.txt', 'config.json', 'logger.log', 'update.tar.xz'] -remove_dir_list = ['.git', '.idea', '__pycache__', 'resources'] +remove_dir_list = ['.git', '.idea', '__pycache__', 'resources', 'tmp'] def main():