diff --git a/config.json b/config.json index d379aac..6e574ee 100644 --- a/config.json +++ b/config.json @@ -4,5 +4,6 @@ "listen_ip": "127.0.0.1", "listen_num": 39, "buffsize": 4096, - "proxy": false + "proxy": false, + "onlyproxy": false } \ No newline at end of file diff --git a/mswp.py b/mswp.py index 7c0a30b..497ece0 100644 --- a/mswp.py +++ b/mswp.py @@ -30,7 +30,7 @@ BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) ID = jsondata.try_to_read_jsondata('id', 'unknown_id') class Datapack: def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'', - file=None, gen_flag=True): + file=None, gen_flag=True, delete=False): self.id = ID if head is None: head = {} @@ -38,6 +38,7 @@ class Datapack: self.head['id'] = self.id self.method = method self.file = file + self.delete = delete self.app = app self.version = version self.body = body @@ -93,6 +94,7 @@ class Datapack: ndp = copy.deepcopy(self) ndp.app = ndp.head['from'] ndp.method = 'reply' + ndp.delete = False if not self.head['id'] == ID: # net package ndp.head['to'] = self.head['id'] ndp.head['id'] = ID diff --git a/plugins/ffmpeg.py b/plugins/ffmpeg.py index 99fd6fa..3002e1a 100644 --- a/plugins/ffmpeg.py +++ b/plugins/ffmpeg.py @@ -24,6 +24,7 @@ class Ffmpeg_controller: self.server = None self.conver_task_queue = queue.Queue() self.org_filename = None + self.concat = True self.conver_task_thread = threading.Thread(target=self.conver_task_func, args=()) self.conver_task_thread.start() @@ -39,32 +40,48 @@ class Ffmpeg_controller: dp = receive_queue.get() if dp.method == 'post' and dp.body == b'start': # config ffmpeg is server or client - self.org_filename = dp.head['filename'] - ndp = dp.reply() - ndp.body = 'Spliting file %s' % dp.head['filename'] - ndp.body = ndp.body.encode() - send_queue.put(ndp) + if dp.head.get('concat'): + if dp.head['concat'] == 'true': + self.concat = True + elif dp.head['concat'] == 'false': + self.concat = False + else: + print('unknown concat value') + continue - cmd = 'ffmpeg -i ' + dp.head['filename'] + ' -c copy -f segment -segment_time 20 \ - -reset_timestamps 1 -y res/ffmpeg_tmp/' + '%d' + '.mp4' + self.org_filename = dp.head['filename'] - os.system(cmd) + if self.concat: + ndp = dp.reply() + ndp.body = 'Spliting file %s' % dp.head['filename'] + ndp.body = ndp.body.encode() + send_queue.put(ndp) + + cmd = 'ffmpeg -i ' + dp.head['filename'] + ' -c copy -f segment -segment_time 20 \ + -reset_timestamps 1 -y res/ffmpeg_tmp/' + '%d' + '.mp4' + + os.system(cmd) self.run_as_server() - # concat all file - filelist = os.listdir('res/ffmpeg_finished') - if 'filelist.txt' in filelist: - filelist.remove('filelist.txt') - with open('res/ffmpeg_finished/filelist.txt', 'w') as f: + if self.concat: + # concat all file + filelist = os.listdir('res/ffmpeg_finished') + if 'filelist.txt' in filelist: + filelist.remove('filelist.txt') + with open('res/ffmpeg_finished/filelist.txt', 'w') as f: + for file in filelist: + f.write('file \'%s\'\n' % file) + object_filename = self.org_filename[:-4] + '.mkv' + subprocess.check_output('ffmpeg -f concat -i res/ffmpeg_finished/filelist.txt \ + -c copy -y ' + object_filename, shell=True) + for file in filelist: - f.write('file \'%s\'\n' % file) - object_filename = self.org_filename[:-4] + '.mkv' - subprocess.check_output('ffmpeg -f concat -i res/ffmpeg_finished/filelist.txt \ - -c copy -y ' + object_filename, shell=True) - - print('All process finished at ' + object_filename) + os.remove('res/ffmpeg_finished/' + file) + os.remove('res/ffmpeg_finished/filelist.txt') + print('All process finished') + elif dp.method == 'post' and dp.body == b'enable': # clinet mode self.status = 1 self.server = dp.head['server'] @@ -175,11 +192,14 @@ class Ffmpeg_controller: 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) + + os.remove(filename) ndp = dp.reply() ndp.head['filename'] = output_filename ndp.head['old_filename'] = filename ndp.method = 'file' + ndp.delete = True send_queue.put(ndp) self.send_request() diff --git a/plugins/net.py b/plugins/net.py index 8b3687d..6cf1c8c 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -15,6 +15,9 @@ receive_queue = receive_queues[__name__] BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) ID = jsondata.try_to_read_jsondata('id', 'Unknown_ID') RETRYSLEEP = 5 +MYPROXY = jsondata.try_to_read_jsondata('proxy', False) +ONLYPROXY = jsondata.try_to_read_jsondata('onlyproxy', False) + def main(): network_controller = Network_controller() @@ -23,6 +26,9 @@ def main(): class Network_controller: # manage id and connection def __init__(self): + if ONLYPROXY and not MYPROXY: + print('config failed because you set onlyproxy true but proxy false') + return self.send_queue = queue.Queue() self.id_dict = {} self.lock = threading.Lock() @@ -153,6 +159,12 @@ class Network_controller: # manage id and connection ip, port = connection.conn.getpeername() port = int(connection.listen_port) connection_list.append((ip, port)) + for addr in self.conflist: + if not addr in connection_list: + connection_list.append(addr) + for addr in self.conflist_pass: + if not addr in connection_list: + connection_list.append(addr) data_dict['mht'] = connection_list data_dict['proxy'] = self.proxydict @@ -209,6 +221,13 @@ class Network_controller: # manage id and connection elif not to: print('not to', dp) + elif ONLYPROXY and not to == MYPROXY: + if dp.head['to']: + dp.head['to'] = to + dp.head['to'] + else: + dp.head['to'] = to + self.send_to_id(MYPROXY, dp) + else: self.send_to_id(to, dp) @@ -349,7 +368,7 @@ class Connection: self.conn.close() return - self.netowrk_controller.set_connection(self,) + self.netowrk_controller.set_connection(self) self.thread_send = threading.Thread(target=self.send_func, args=(), daemon=True) self.thread_send.start() @@ -467,6 +486,13 @@ class Connection: #print('you connect to your self') return 4, dp.head.get('flag') + if ONLYPROXY and not self.id == MYPROXY: # refuce not proxy connection + return 5, dp.head.get('flag') + + if dp.head.get('onlyuseproxy'): + if not dp.head['onlyuseproxy'] == ID: + return 6, dp.head.get('flag') + if not self.positive: self.send_id() @@ -476,6 +502,8 @@ class Connection: def send_id(self): dp = Datapack(head={'from': __name__}) dp.app = 'handshake' + if ONLYPROXY: + dp.head['onlyuseproxy'] = MYPROXY dp.head['listen_port'] = str(jsondata.try_to_read_jsondata('listen_port', 3900)) dp.encode() self.conn.sendall(dp.encode_data) @@ -497,8 +525,14 @@ class Connection: self.conn.sendall(data) except Exception as e: print('Failed to send file %s %s: %s' % (dp.head['filename'], type(e), str(e)), dp) + if dp.head.get('to'): + dp.head['to'] = self.id + '&' + dp.head['to'] + else: + dp.head['to'] = self.id self.netowrk_controller.wheel_queue.put(dp) break + if dp.delete: + os.remove(dp.head['filename']) print('Send file %s finished' % dp.head['filename'], dp)