Skip to content

Commit

Permalink
embed subtitles implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ilstam committed Aug 6, 2014
1 parent 24e4444 commit 2ae25cf
Showing 1 changed file with 96 additions and 39 deletions.
135 changes: 96 additions & 39 deletions ffmulticonverter/audiovideotab.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
from PyQt4.QtGui import (
QApplication, QWidget, QComboBox, QLineEdit, QLabel, QRegExpValidator,
QPushButton, QCheckBox, QRadioButton, QHBoxLayout, QSpacerItem,
QSizePolicy, QFrame, QButtonGroup, QMessageBox, QToolButton
QSizePolicy, QFrame, QButtonGroup, QMessageBox, QToolButton,
QFileDialog
)

from ffmulticonverter import utils
from ffmulticonverter import presets_dlgs
from ffmulticonverter import config


class AudioVideoTab(QWidget):
Expand Down Expand Up @@ -166,17 +168,16 @@ def __init__(self, parent):

embedQL = QLabel(self.tr("Embed subtitle:"))
self.embedQLE = QLineEdit()
self.embedQLE.setReadOnly(True)
embedToolButton = QToolButton()
embedToolButton.setText("...")
self.embedQTB = QToolButton()
self.embedQTB.setText("...")

rotateQL = QLabel(self.tr("Rotate:"))
self.rotateQCB = QComboBox()
self.rotateQCB.addItems(rotation_options)

hlayout5 = utils.add_to_layout(
'h', rotateQL, self.rotateQCB, embedQL, self.embedQLE,
embedToolButton)
self.embedQTB)

hidden_layout = utils.add_to_layout(
'v', videosettings_layout, audiosettings_layout,
Expand All @@ -202,6 +203,7 @@ def __init__(self, parent):

self.presetQPB.clicked.connect(self.choose_preset)
self.defaultQPB.clicked.connect(self.set_default_command)
self.embedQTB.clicked.connect(self.open_subtitle_file)
self.moreQPB.toggled.connect(self.frame.setVisible)
self.moreQPB.toggled.connect(self.resize_parent)
self.widthQLE.textChanged.connect(self.command_update_size)
Expand All @@ -213,6 +215,7 @@ def __init__(self, parent):
self.threadsQLE.textChanged.connect(self.command_update_threads)
self.beginQLE.textChanged.connect(self.command_update_begin_time)
self.durationQLE.textChanged.connect(self.command_update_duration)
self.embedQLE.textChanged.connect(self.command_update_subtitles)
self.vidcodecQCB.currentIndexChanged.connect(self.command_update_vcodec)
self.audcodecQCB.currentIndexChanged.connect(self.command_update_acodec)
self.freqQCB.currentIndexChanged.connect(self.command_update_frequency)
Expand All @@ -228,27 +231,6 @@ def __init__(self, parent):
self.preservesizeQChB.toggled.connect(
self.command_update_preserve_size)

def fill_video_comboboxes(self, videocodecs, audiocodecs, extraformats):
if videocodecs:
videocodecs = [i for i in videocodecs.split("\n")]
else:
videocodecs = []
if audiocodecs:
audiocodecs = [i for i in audiocodecs.split("\n")]
else:
audiocodecs = []
if extraformats:
extraformats = [i for i in extraformats.split("\n")]
else:
extraformats = []

self.vidcodecQCB.clear()
self.audcodecQCB.clear()
self.extQCB.clear()
self.vidcodecQCB.addItems([self.defaultStr] + videocodecs)
self.audcodecQCB.addItems([self.defaultStr] + audiocodecs)
self.extQCB.addItems(sorted(self.formats + extraformats))

def resize_parent(self):
"""Resize MainWindow."""
if self.frame.isVisible():
Expand Down Expand Up @@ -283,6 +265,27 @@ def clear(self):
# setExclusive(False) in order to be able to uncheck checkboxes and
# then setExclusive(True) so only one radio button can be set

def fill_video_comboboxes(self, videocodecs, audiocodecs, extraformats):
if videocodecs:
videocodecs = [i for i in videocodecs.split("\n")]
else:
videocodecs = []
if audiocodecs:
audiocodecs = [i for i in audiocodecs.split("\n")]
else:
audiocodecs = []
if extraformats:
extraformats = [i for i in extraformats.split("\n")]
else:
extraformats = []

self.vidcodecQCB.clear()
self.audcodecQCB.clear()
self.extQCB.clear()
self.vidcodecQCB.addItems([self.defaultStr] + videocodecs)
self.audcodecQCB.addItems([self.defaultStr] + audiocodecs)
self.extQCB.addItems(sorted(self.formats + extraformats))

def ok_to_continue(self):
"""
Check if everything is ok with audiovideotab to continue conversion.
Expand All @@ -300,6 +303,15 @@ def ok_to_continue(self):
return False
return True

def open_subtitle_file(self):
"""
Get the filename using standard QtDialog and update embedQLE's text.
"""
fname = QFileDialog.getOpenFileName(self, 'FF Multi Converter - ' +
self.tr('Choose File'), config.home, 'Subtitles (*.srt *.sub)')
if fname:
self.embedQLE.setText(fname)

def set_default_command(self):
"""Set the default value to self.commandQLE."""
self.clear()
Expand Down Expand Up @@ -554,33 +566,78 @@ def command_update_acodec(self):
self.commandQLE.clear()
self.commandQLE.setText(command)

def command_update_subtitles(self):
command = self.commandQLE.text()
text = self.embedQLE.text()

regex1 = r'(,*\s*){0,1}(subtitles=\'.*\')(,*\s*){0,1}'
regex2 = r'(-vf "[^"]*)"'

s = "subtitles='{0}'".format(text) if text else ''

search = re.search(regex1, command)
if search:
if text:
command = re.sub(regex1, r'\1{0}\3'.format(s), command)
else:
if search.groups()[0] and search.groups()[3]:
command = re.sub(regex1, ', ', command)
else:
command = re.sub(regex1, s, command)
elif re.search(regex2, command):
command = re.sub(regex2, r'\1, {0}"'.format(s), command)
else:
command += ' -vf "' + s + '"'

if not text:
command = re.sub(r'-vf "\s*"', '', command)

command = re.sub(' +', ' ', command).strip()

self.commandQLE.clear()
self.commandQLE.setText(command)

def command_update_rotation(self):
command = self.commandQLE.text()
rotate = self.rotateQCB.currentIndex()

if rotate == 0: # none
s = ' '
s = ''
elif rotate == 1: # 90 clockwise
s = ' -vf "transpose=1" '
s = 'transpose=1'
elif rotate == 2: # 90 clockwise + vertical flip
s = ' -vf "transpose=3" '
s = 'transpose=3'
elif rotate == 3: # 90 counter clockwise
s = ' -vf "transpose=2" '
s = 'transpose=2'
elif rotate == 4: # 90 counter clockwise + vertical flip
s = ' -vf "transpose=0" '
s = 'transpose=0'
elif rotate == 5: # 180
s = ' -vf "transpose=2,transpose=2" '
s = 'transpose=2, transpose=2'
elif rotate == 6: # horizontal flip
s = ' -vf hflip '
s = 'hflip'
elif rotate == 7: # vertical flip
s = ' -vf vflip '
s = 'vflip'

regex1 = r'(,*\s*){0,1}(transpose=\d(,\s*transpose=\d)*|vflip|hflip)(,*\s*){0,1}'
regex2 = r'(-vf "[^"]*)"'

search = re.search(regex1, command)
if search:
if rotate != 0:
command = re.sub(regex1, r'\1{0}\4'.format(s), command)
else:
if search.groups()[0] and search.groups()[3]:
command = re.sub(regex1, ', ', command)
else:
command = re.sub(regex1, s, command)
elif re.search(regex2, command):
command = re.sub(regex2, r'\1, {0}"'.format(s), command)
else:
command += ' -vf "' + s + '"'

regex = r'(\s+|^)-vf\s+(transpose=\d(,transpose=\d)*|"\s*transpose.*"|hflip|vflip|"\s*hflip.*"|"\s*vflip.*")(\s+|$)'
if rotate == 0:
command = re.sub(r'-vf "\s*"', '', command)

if re.search(regex, command):
command = re.sub(regex, s, command)
else:
command += s
command = re.sub(' +', ' ', command).strip()

self.commandQLE.clear()
Expand Down

0 comments on commit 2ae25cf

Please sign in to comment.