ffmpeg and file bug fix
This commit is contained in:
@@ -61,7 +61,7 @@ class Jsondata:
|
|||||||
def create_floder(path):
|
def create_floder(path):
|
||||||
pathlist = list(os.path.split(path))
|
pathlist = list(os.path.split(path))
|
||||||
pathlist.pop()
|
pathlist.pop()
|
||||||
flordpath = os.path.join(pathlist)
|
flordpath = '/'.join(pathlist)
|
||||||
|
|
||||||
if not os.path.exists(flordpath):
|
if not os.path.exists(flordpath):
|
||||||
_create_floder(flordpath)
|
_create_floder(flordpath)
|
||||||
@@ -69,11 +69,12 @@ def create_floder(path):
|
|||||||
def _create_floder(path):
|
def _create_floder(path):
|
||||||
pathlist = list(os.path.split(path))
|
pathlist = list(os.path.split(path))
|
||||||
pathlist.pop()
|
pathlist.pop()
|
||||||
flordpath = os.path.join(pathlist)
|
flordpath = '/'.join(pathlist)
|
||||||
|
|
||||||
if not os.path.exists(flordpath):
|
if not os.path.exists(flordpath):
|
||||||
_create_floder(flordpath)
|
_create_floder(flordpath)
|
||||||
os.mkdir(flordpath)
|
if not os.path.exists(path):
|
||||||
|
os.mkdir(path)
|
||||||
|
|
||||||
global_config = {}
|
global_config = {}
|
||||||
msw_queue = queue.Queue()
|
msw_queue = queue.Queue()
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import copy
|
|||||||
import os
|
import os
|
||||||
from mswp import Datapack
|
from mswp import Datapack
|
||||||
from forwarder import receive_queues, send_queue
|
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
|
from config import dprint as print
|
||||||
receive_queue = receive_queues[__name__]
|
receive_queue = receive_queues[__name__]
|
||||||
|
|
||||||
@@ -17,33 +17,126 @@ def main():
|
|||||||
class Ffmpeg_controller:
|
class Ffmpeg_controller:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.ffmpeg_type = None
|
self.ffmpeg_type = None
|
||||||
self.status = 'disable'
|
self.status = 0
|
||||||
self.padding_to_convert = []
|
self.server = None
|
||||||
self.already_in_convert = []
|
|
||||||
self.finished_convert = []
|
|
||||||
self.mainloop()
|
self.mainloop()
|
||||||
|
|
||||||
|
|
||||||
def mainloop(self):
|
def mainloop(self):
|
||||||
|
_create_floder('resources/ffmpeg_tmp')
|
||||||
|
_create_floder('resources/ffmpeg_finished')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
dp = receive_queue.get()
|
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 = dp.reply()
|
||||||
ndp.body = 'Spliting file %s' % dp.head['filename']
|
ndp.body = 'Spliting file %s' % dp.head['filename']
|
||||||
ndp.body = ndp.body.encode()
|
ndp.body = ndp.body.encode()
|
||||||
send_queue.put(ndp)
|
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 \
|
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)
|
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 = threading.Thread(target=main, args=(), daemon=True)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|||||||
@@ -370,11 +370,13 @@ class Connection:
|
|||||||
|
|
||||||
if dp.method == 'file':
|
if dp.method == 'file':
|
||||||
create_floder(dp.head['filename'])
|
create_floder(dp.head['filename'])
|
||||||
|
create_floder('tmp/' + dp.head['filename'])
|
||||||
if dp.method == 'file' and os.path.exists(dp.head['filename']):
|
if dp.method == 'file' and os.path.exists(dp.head['filename']):
|
||||||
os.remove(dp.head['filename'])
|
os.remove(dp.head['filename'])
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Decode head failed %s: %s' % (type(e), str(e)))
|
print('Decode head failed %s: %s' % (type(e), str(e)))
|
||||||
|
print(self.buff)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
length = int(dp.head.get('length'))
|
length = int(dp.head.get('length'))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ receive_queue = receive_queues[__name__]
|
|||||||
|
|
||||||
|
|
||||||
remove_file_list = ['__init__.py', 'addrlist.txt', 'config.json', 'logger.log', 'update.tar.xz']
|
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():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user