Skip to content

Commit

Permalink
✨ add multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
taseikyo committed Oct 9, 2019
1 parent c51ab16 commit 550cf30
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ffmpeg-helper/ffmpeg-helper/mwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ MWin::~MWin()
{
delete ui;
}

void MWin::on_duration_check_stateChanged(int arg1)
{

}
13 changes: 13 additions & 0 deletions ffmpeg-helper/ffmpeg-helper/mwin.ui
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="log_edit"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
Expand All @@ -191,6 +194,16 @@
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="extract_btn">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Extract Audio</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="start_btn">
<property name="minimumSize">
Expand Down
58 changes: 53 additions & 5 deletions ffmpeg-helper/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
import sys
import subprocess

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
Expand All @@ -28,6 +29,11 @@ def __init__(self, parent=None):

self.mode = DURATION_MODE

self.cmder_thread = Cmder('')
self.cmder_thread.log.connect(self.log_display)
self.cmder_thread.done.connect(self.log_display)
self.cmder_thread.error.connect(self.error_handler)

@pyqtSlot()
def on_select_file_btn_clicked(self):
filename, filetype = QFileDialog.getOpenFileName(self,
Expand All @@ -38,6 +44,7 @@ def on_select_file_btn_clicked(self):

self.cut_file_path = filename
self.filename_label.setText(os.path.basename(filename))
self.log_edit.setPlainText('')

def on_duration_check_stateChanged(self, mode):
if mode == 0:
Expand Down Expand Up @@ -76,13 +83,20 @@ def on_start_btn_clicked(self):
else:
cmd = f'ffmpeg -ss {start_offset} -i "{self.cut_file_path}" -vcodec copy -acodec copy -t {end_offset} "{out_file}" -y'

try:
with os.popen(cmd) as f:
print(f.read())
except Exception as e:
print(e)
self.cmder_thread.cmd = cmd
self.cmder_thread.start()

@pyqtSlot()
def on_extract_btn_clicked(self):
if not self.cut_file_path:
return

path, file = os.path.split(self.cut_file_path)

cmd = f'''ffmpeg -i "{self.cut_file_path}" -vn -y -acodec copy "{path}/{file.split('.')[0]}.m4a"'''
self.cmder_thread.cmd = cmd
self.cmder_thread.start()

def time_format_check(self, time_raw):
'''check `time_raw` is legal
and return a legal time
Expand Down Expand Up @@ -176,6 +190,40 @@ def on_start_merge_btn_clicked(self):
os.chdir(cwd)


def log_display(self, text):
old_text = self.log_edit.toPlainText()
self.log_edit.setPlainText(f'{old_text}{text}')

scrollbar = self.log_edit.verticalScrollBar()
if scrollbar:
scrollbar.setSliderPosition(scrollbar.maximum())

def error_handler(self, emsg):
QMessageBox.warning(self, 'FFmpeg Helper', emsg, QMessageBox.Ok)

class Cmder(QThread):
log = pyqtSignal(str)
error = pyqtSignal(str)
done = pyqtSignal(str)

def __init__(self, cmd):
super().__init__()
self.cmd = cmd

def run(self):
try:
p = subprocess.Popen(self.cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
try:
line = line.decode('utf-8')
except:
line = line.decode('gbk')
self.log.emit(line)
except Exception as e:
self.error.emit(str(e))
self.done.emit('done')


if __name__ == '__main__':
app = QApplication(sys.argv)
w = MWin()
Expand Down

0 comments on commit 550cf30

Please sign in to comment.