thread_exec_autosub.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. '''
  2. (C) 2019 Raryel C. Souza
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. '''
  14. from PyQt5.QtCore import QThread
  15. from PyQt5.QtCore import pyqtSignal
  16. from pathlib import Path
  17. from pytranscriber.util.srtparser import SRTParser
  18. from pytranscriber.util.util import MyUtil
  19. from pytranscriber.control.ctr_autosub import Ctr_Autosub
  20. import os
  21. class Thread_Exec_Autosub(QThread):
  22. signalLockGUI = pyqtSignal()
  23. signalResetGUIAfterCancel = pyqtSignal()
  24. signalResetGUIAfterSuccess = pyqtSignal()
  25. signalProgress = pyqtSignal(str, int)
  26. signalProgressFileYofN = pyqtSignal(str)
  27. signalErrorMsg = pyqtSignal(str)
  28. def __init__(self, objParamAutosub):
  29. self.objParamAutosub = objParamAutosub
  30. self.running = True
  31. QThread.__init__(self)
  32. def __updateProgressFileYofN(self, currentIndex, countFiles ):
  33. self.signalProgressFileYofN.emit("File " + str(currentIndex+1) + " of " +str(countFiles))
  34. def listenerProgress(self, string, percent):
  35. self.signalProgress.emit(string, percent)
  36. def __generatePathOutputFile(self, sourceFile):
  37. #extract the filename without extension from the path
  38. base = os.path.basename(sourceFile)
  39. #[0] is filename, [1] is file extension
  40. fileName = os.path.splitext(base)[0]
  41. #the output file has same name as input file, located on output Folder
  42. #with extension .srt
  43. pathOutputFolder = Path(self.objParamAutosub.outputFolder)
  44. outputFileSRT = pathOutputFolder / (fileName + ".srt")
  45. outputFileTXT = pathOutputFolder / (fileName + ".txt")
  46. return [outputFileSRT, outputFileTXT]
  47. def __runAutosubForMedia(self, index, langCode):
  48. sourceFile = self.objParamAutosub.listFiles[index]
  49. outputFiles = self.__generatePathOutputFile(sourceFile)
  50. outputFileSRT = outputFiles[0]
  51. outputFileTXT = outputFiles[1]
  52. #run autosub
  53. fOutput = Ctr_Autosub.generate_subtitles(source_path = sourceFile,
  54. output = outputFileSRT,
  55. src_language = langCode,
  56. listener_progress = self.listenerProgress)
  57. #if nothing was returned
  58. if not fOutput:
  59. self.signalErrorMsg.emit("Error! Unable to generate subtitles for file " + sourceFile + ".")
  60. elif fOutput != -1:
  61. #if the operation was not canceled
  62. #updated the progress message
  63. self.listenerProgress("Finished", 100)
  64. #parses the .srt subtitle file and export text to .txt file
  65. SRTParser.extractTextFromSRT(str(outputFileSRT))
  66. if self.objParamAutosub.boolOpenOutputFilesAuto:
  67. #open both SRT and TXT output files
  68. MyUtil.open_file(outputFileTXT)
  69. MyUtil.open_file(outputFileSRT)
  70. def __loopSelectedFiles(self):
  71. self.signalLockGUI.emit()
  72. langCode = self.objParamAutosub.langCode
  73. #if output directory does not exist, creates it
  74. pathOutputFolder = Path(self.objParamAutosub.outputFolder)
  75. if not os.path.exists(pathOutputFolder):
  76. os.mkdir(pathOutputFolder)
  77. #if there the output file is not a directory
  78. if not os.path.isdir(pathOutputFolder):
  79. #force the user to select a different output directory
  80. self.signalErrorMsg.emit("Error! Invalid output folder. Please choose another one.")
  81. else:
  82. #go ahead with autosub process
  83. nFiles = len(self.objParamAutosub.listFiles)
  84. for i in range(nFiles):
  85. #does not continue the loop if user clicked cancel button
  86. if not Ctr_Autosub.is_operation_canceled():
  87. self.__updateProgressFileYofN(i, nFiles)
  88. self.__runAutosubForMedia(i, langCode)
  89. #if operation is canceled does not clear the file list
  90. if Ctr_Autosub.is_operation_canceled():
  91. self.signalResetGUIAfterCancel.emit()
  92. else:
  93. self.signalResetGUIAfterSuccess.emit()
  94. def run(self):
  95. Ctr_Autosub.init()
  96. self.__loopSelectedFiles()
  97. self.running = False
  98. def cancel(self):
  99. Ctr_Autosub.cancel_operation()