/// <summary> /// Does the work of the preview thread. Performs the processing on subtitles. /// </summary> private void bw_DoWork(object sender, DoWorkEventArgs e) { WorkerVars workerVars = e.Argument as WorkerVars; List <List <InfoCombined> > combinedAll = new List <List <InfoCombined> >(); WorkerSubs subsWorker = new WorkerSubs(); int totalLines = 0; // Parse and combine the subtitles try { combinedAll = subsWorker.combineAllSubs(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong before processing could start.\n" + e1); e.Cancel = true; return; } foreach (List <InfoCombined> combArray in workerVars.CombinedAll) { totalLines += combArray.Count; } if (totalLines == 0) { UtilsMsg.showErrMsg("No lines of dialog could be parsed from the subtitle files.\nPlease check that they are valid."); e.Cancel = true; return; } // Remove lines try { combinedAll = subsWorker.inactivateLines(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while removing lines.\n" + e1); e.Cancel = true; return; } totalLines = 0; foreach (List <InfoCombined> combArray in workerVars.CombinedAll) { totalLines += combArray.Count; } if (totalLines == 0) { UtilsMsg.showErrMsg("No lines will be processed. Please check your settings to make\nsure that you are not mistakenly pruning too many lines."); e.Cancel = true; return; } e.Result = workerVars; }
/// <summary> /// Performs the work of the audio extraction thread. /// </summary> private void splitAudio(object sender, DoWorkEventArgs e) { WorkerVars workerVars = e.Argument as WorkerVars; List <List <InfoCombined> > combinedAll = new List <List <InfoCombined> >(); WorkerSubs subsWorker = new WorkerSubs(); if (groupBoxCheckLyrics.Checked) { // Parse and combine the subtitles try { combinedAll = subsWorker.combineAllSubs(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong before processing could start.\n" + e1); e.Cancel = true; return; } } DateTime mediaStartime = new DateTime(); DateTime mediaEndtime = new DateTime(); DateTime mediaDuration = new DateTime(); int episode = 0; DialogProgress.updateProgressInvoke(dialogProgress, 0, "Starting..."); foreach (string file in mediaFiles) { episode++; try { mediaEndtime = UtilsVideo.getVideoLength(file); } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while determining duration of the media:\n" + e1); return; } if (useSpan) { mediaStartime = spanStart; // If the span end time if not greater than the actual duration of the media if (spanEnd < mediaEndtime) { mediaEndtime = spanEnd; } } UtilsName name = new UtilsName( deckName, mediaFiles.Length, // Total number of episodes 1, // Total number of lines (Note: not filled out here) mediaEndtime, // Last time 0, 0 // Width and height (Note: not filled out anywhere) ); mediaDuration = UtilsSubs.getDurationTime(mediaStartime, mediaEndtime); string progressText = String.Format("Processing audio from media file {0} of {1}", episode, mediaFiles.Length); int progress = Convert.ToInt32((episode - 1) * (100.0 / mediaFiles.Length)); DialogProgress.updateProgressInvoke(dialogProgress, progress, progressText); // Enable detail mode in progress dialog DialogProgress.enableDetailInvoke(dialogProgress, true); // Set the duration of the clip in the progress dialog (for detail mode) DialogProgress.setDuration(dialogProgress, mediaDuration); string tempMp3Filename = Path.GetTempPath() + ConstantSettings.TempAudioFilename; UtilsAudio.ripAudioFromVideo(mediaFiles[episode - 1], audioStream.Num, mediaStartime, mediaEndtime, bitrate, tempMp3Filename, dialogProgress); DialogProgress.enableDetailInvoke(dialogProgress, false); if (dialogProgress.Cancel) { e.Cancel = true; File.Delete(tempMp3Filename); return; } int numClips = 1; if (!isSingleFile) { numClips = (int)Math.Ceiling((mediaDuration.TimeOfDay.TotalMilliseconds / (clipLength.TimeOfDay.TotalSeconds * 1000.0))); } for (int clipIdx = 0; clipIdx < numClips; clipIdx++) { progressText = String.Format("Splitting segment {0} of {1} from media file {2} of {3}", clipIdx + 1, numClips, episode, mediaFiles.Length); progress = Convert.ToInt32((episode - 1) * (100.0 / mediaFiles.Length)); DialogProgress.updateProgressInvoke(dialogProgress, progress, progressText); if (dialogProgress.Cancel) { e.Cancel = true; File.Delete(tempMp3Filename); return; } // The start and end times used for processing DateTime startTime = new DateTime(); DateTime endTime = new DateTime(); if (isSingleFile) { endTime = mediaDuration; } else { startTime = startTime.AddSeconds((double)(clipLength.TimeOfDay.TotalSeconds * clipIdx)); endTime = endTime.AddSeconds((double)(clipLength.TimeOfDay.TotalSeconds * (clipIdx + 1))); if (endTime.TimeOfDay.TotalMilliseconds >= mediaDuration.TimeOfDay.TotalMilliseconds) { endTime = mediaDuration; } } // The start and end times that will be displayed DateTime startTimeName = startTime.AddMilliseconds(mediaStartime.TimeOfDay.TotalMilliseconds); DateTime endTimeName = endTime.AddMilliseconds(mediaStartime.TimeOfDay.TotalMilliseconds); // Fill in the total number of lines with the total number of clips name.TotalNumLines = numClips; string nameStr = name.createName(ConstantSettings.ExtractMediaAudioFilenameFormat, episode + episodeStartNumber - 1, clipIdx + 1, startTimeName, endTimeName, "", ""); string outName = String.Format("{0}{1}{2}", outputDir, // {0} Path.DirectorySeparatorChar, // {1} nameStr); // {2} UtilsAudio.cutAudio(tempMp3Filename, startTime, endTime, outName); nameStr = name.createName(ConstantSettings.AudioId3Artist, episode + episodeStartNumber - 1, clipIdx + 1, startTimeName, endTimeName, "", ""); string tagArtist = String.Format("{0}", nameStr); // {0} nameStr = name.createName(ConstantSettings.AudioId3Album, episode + episodeStartNumber - 1, clipIdx + 1, startTimeName, endTimeName, "", ""); string tagAlbum = String.Format("{0}", nameStr); // {0} nameStr = name.createName(ConstantSettings.AudioId3Title, episode + episodeStartNumber - 1, clipIdx + 1, startTimeName, endTimeName, "", ""); string tagTitle = String.Format("{0}", nameStr); // {0} nameStr = name.createName(ConstantSettings.AudioId3Genre, episode + episodeStartNumber - 1, clipIdx + 1, startTimeName, endTimeName, "", ""); string tagGenre = String.Format("{0}", nameStr); // {0} string tagLyrics = ""; if (groupBoxCheckLyrics.Checked) { int totalLyricsLines = 0; int curLyricsNum = 1; // Precount the number of lyrics lines foreach (InfoCombined comb in combinedAll[episode - 1]) { if (comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds >= startTimeName.TimeOfDay.TotalMilliseconds && comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds <= endTimeName.TimeOfDay.TotalMilliseconds) { totalLyricsLines++; } } // Fill in the total number of lyrics lines name.TotalNumLines = curLyricsNum; // Foreach comb in the current episode, if the comb lies within the // current clip, add it to the lryics tag foreach (InfoCombined comb in combinedAll[episode - 1]) { if (comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds >= startTimeName.TimeOfDay.TotalMilliseconds && comb.Subs1.StartTime.TimeOfDay.TotalMilliseconds <= endTimeName.TimeOfDay.TotalMilliseconds) { tagLyrics += formatLyricsPair(comb, name, startTimeName, episode + episodeStartNumber - 1, curLyricsNum) + "\r\n"; curLyricsNum++; } } } UtilsAudio.tagAudio(outName, tagArtist, tagAlbum, tagTitle, tagGenre, tagLyrics, clipIdx + 1, numClips); } } return; }
/// <summary> /// Performs the work in the processing thread. /// </summary> private void bw_DoWork(object sender, DoWorkEventArgs e) { WorkerVars workerVars = e.Argument as WorkerVars; List <List <InfoCombined> > combinedAll = new List <List <InfoCombined> >(); WorkerSubs subsWorker = new WorkerSubs(); int totalLines = 0; bool needToGenerateCombinedAll = workerVars.CombinedAll == null; // Only generate a combinedAll if one if not provided if (needToGenerateCombinedAll) { // Parse and combine the subtitles try { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Combine subs"); combinedAll = subsWorker.combineAllSubs(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong before processing could start.\n" + e1); e.Cancel = true; return; } foreach (List <InfoCombined> combArray in workerVars.CombinedAll) { totalLines += combArray.Count; } if (totalLines == 0) { UtilsMsg.showErrMsg( "No lines of dialog could be parsed from the subtitle files.\nPlease check that they are valid."); e.Cancel = true; return; } // Inactivate lines try { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Inactivate lines"); combinedAll = subsWorker.inactivateLines(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while setting active lines.\n" + e1); e.Cancel = true; return; } } // Find context lines if (Settings.Instance.ContextLeadingCount > 0 || Settings.Instance.ContextTrailingCount > 0) { try { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Find context lines"); combinedAll = subsWorker.markLinesOnlyNeededForContext(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while finding context lines.\n" + e1); e.Cancel = true; return; } } // Remove Inactive lines (unless they are needed for context) try { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Remove inactive lines"); combinedAll = subsWorker.removeInactiveLines(workerVars, dialogProgress, true); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while removing inactive lines.\n" + e1); e.Cancel = true; return; } totalLines = 0; foreach (List <InfoCombined> combArray in workerVars.CombinedAll) { totalLines += combArray.Count; } if (totalLines == 0) { UtilsMsg.showErrMsg( "No lines will be processed. Please check your settings to make\nsure that you are not mistakenly pruning too many lines."); e.Cancel = true; return; } try { // Move vobsubs from preview dir to .media dir if (!needToGenerateCombinedAll) { if (!subsWorker.copyVobsubsFromPreviewDirToMediaDir(workerVars, dialogProgress)) { e.Cancel = true; return; } } } catch { } // Generate SRS import file try { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Generate import file"); WorkerSrs srsWorker = new WorkerSrs(); if (!srsWorker.genSrs(workerVars, dialogProgress)) { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while generating the SRS import file.\n" + e1); e.Cancel = true; return; } List <List <InfoCombined> > combinedAllWithContext = ObjectCopier.Clone <List <List <InfoCombined> > >(workerVars.CombinedAll); // Generate audio clips try { if (Settings.Instance.AudioClips.Enabled) { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Generate audio clips"); if (Settings.Instance.ContextLeadingCount > 0 && Settings.Instance.ContextLeadingIncludeAudioClips || Settings.Instance.ContextTrailingCount > 0 && Settings.Instance.ContextTrailingIncludeAudioClips) { workerVars.CombinedAll = combinedAllWithContext; } else { workerVars.CombinedAll = subsWorker.removeContextOnlyLines(combinedAllWithContext); } WorkerAudio audioWorker = new WorkerAudio(); if (!audioWorker.genAudioClip(workerVars, dialogProgress)) { e.Cancel = true; return; } } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while generating the audio clips.\n" + e1); e.Cancel = true; return; } // Generate Snapshots try { if (Settings.Instance.Snapshots.Enabled) { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Generate snapshots"); if (Settings.Instance.ContextLeadingCount > 0 && Settings.Instance.ContextLeadingIncludeSnapshots || Settings.Instance.ContextTrailingCount > 0 && Settings.Instance.ContextTrailingIncludeSnapshots) { workerVars.CombinedAll = combinedAllWithContext; } else { workerVars.CombinedAll = subsWorker.removeContextOnlyLines(combinedAllWithContext); } WorkerSnapshot snapshotWorker = new WorkerSnapshot(); if (!snapshotWorker.genSnapshots(workerVars, dialogProgress)) { e.Cancel = true; return; } } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while generating snapshots.\n" + e1); e.Cancel = true; return; } // Generate video clips try { if (Settings.Instance.VideoClips.Enabled) { DialogProgress.nextStepInvoke(dialogProgress, ++currentStep, "Generate video clips"); if (Settings.Instance.ContextLeadingCount > 0 && Settings.Instance.ContextLeadingIncludeVideoClips || Settings.Instance.ContextTrailingCount > 0 && Settings.Instance.ContextTrailingIncludeVideoClips) { workerVars.CombinedAll = combinedAllWithContext; } else { workerVars.CombinedAll = subsWorker.removeContextOnlyLines(combinedAllWithContext); } WorkerVideo videoWorker = new WorkerVideo(); if (!videoWorker.genVideoClip(workerVars, dialogProgress)) { e.Cancel = true; return; } } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong while generating the video clips.\n" + e1); e.Cancel = true; return; } e.Result = workerVars; }
/// <summary> /// Perform the work in the Dueling Subtitles creation thread. /// </summary> private void bw_DoWork(object sender, DoWorkEventArgs e) { WorkerVars workerVars = e.Argument as WorkerVars; List <List <InfoCombined> > combinedAll = new List <List <InfoCombined> >(); WorkerSubs subsWorker = new WorkerSubs(); // Parse and combine the subtitles try { combinedAll = subsWorker.combineAllSubs(workerVars, dialogProgress); if (combinedAll != null) { workerVars.CombinedAll = combinedAll; } else { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong before processing could start.\n" + e1); e.Cancel = true; return; } // Create the .ass file try { if (!createDuelingSubtitles(workerVars, dialogProgress)) { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong when generating the Dueling Subtitles.\n" + e1); e.Cancel = true; return; } // Create the quick reference file if (quickReference) { try { if (!createQuickReference(workerVars, dialogProgress)) { e.Cancel = true; return; } } catch (Exception e1) { UtilsMsg.showErrMsg("Something went wrong when generating the quick reference file.\n" + e1); e.Cancel = true; return; } } }