From 6e1b9b76694cea705f87d9bd7a11619bc4fdcfcb Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Wed, 1 Apr 2020 21:53:05 +0800 Subject: [PATCH] new function ffmpeg shell --- plugins/ffmpeg.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ plugins/shell.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 plugins/ffmpeg.py create mode 100644 plugins/shell.py diff --git a/plugins/ffmpeg.py b/plugins/ffmpeg.py new file mode 100644 index 0000000..7d9f1cc --- /dev/null +++ b/plugins/ffmpeg.py @@ -0,0 +1,49 @@ +import threading +import copy +import os +from mswp import Datapack +from forwarder import receive_queues, send_queue +from config import msw_queue +from config import dprint as print +receive_queue = receive_queues[__name__] + + +def main(): + while True: + ffmpeg_controller = Ffmpeg_controller() + ffmpeg_controller.mainloop() + + +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.mainloop() + + + def mainloop(self): + while True: + dp = receive_queue.get() + + if dp.method == 'post' and dp.body == b'split': # 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' + + os.system(cmd) + + + + +thread = threading.Thread(target=main, args=(), daemon=True) +thread.start() diff --git a/plugins/shell.py b/plugins/shell.py new file mode 100644 index 0000000..531c10e --- /dev/null +++ b/plugins/shell.py @@ -0,0 +1,35 @@ +import threading +import copy +import os +import subprocess +from mswp import Datapack +from forwarder import receive_queues, send_queue +from config import msw_queue +from config import dprint as print +receive_queue = receive_queues[__name__] + + +def main(): + while True: + dp = receive_queue.get() + command = dp.body.decode() + try: + result = subprocess.check_output(command, shell=True) + except Exception as e: + result = 'Command %s error, %s: %s' % (command, type(e), str(e)) + result = result.encode() + + ndp = dp.reply() + ndp.body = try_decode_and_encode(result) + send_queue.put(ndp) + + +def try_decode_and_encode(data): + try: + return data.decode('gb2312').encode() + except: + return data.decode('utf-8').encode() + +thread = threading.Thread(target=main, args=(), daemon=True) +thread.start() +