public void OpenTranslation(string path, Encoding encoding) { if (this.IsTranslationLoaded) { CloseTranslation(); } SubtitleFactory factory = new SubtitleFactory(); factory.Encoding = encoding; factory.FallbackEncoding = GetFallbackEncoding(); SubLib.Core.Domain.Subtitles openedTranslation = factory.Open(path); FileProperties newTranslationFile = factory.FileProperties; AddExtraSubtitles(openedTranslation); Translations translations = new Translations(); translations.Import(subtitles, openedTranslation, Base.Config.TimingsTimeBetweenSubtitles); if (newTranslationFile.SubtitleType != SubtitleType.Unknown) { canTranslationBeSaved = true; } this.translationFile = newTranslationFile; this.isTranslationLoaded = true; }
/* Used in the object construction */ private void Open(string path, Encoding encoding) { SubtitleFactory factory = new SubtitleFactory(); factory.Encoding = encoding; factory.FallbackEncoding = GetFallbackEncoding(); factory.InputFrameRate = Base.Ui.Menus.TimingsInputFrameRateActive; SubLib.Core.Domain.Subtitles openedSubtitles = null; try { openedSubtitles = factory.Open(path); } catch (FileNotFoundException) { New(path); return; } subtitles = new Ui.View.Subtitles(openedSubtitles); textFile = factory.FileProperties; if (textFile.SubtitleType != SubtitleType.Unknown) { canTextBeSaved = true; } }
/// <summary>Copies the translation to the subtitles.</summary> private void CopyTranslation (Subtitles subtitles, Subtitles translation) { for (int subtitleNumber = 0 ; subtitleNumber < translation.Collection.Count ; subtitleNumber++) { Subtitle translated = translation.Collection[subtitleNumber]; Subtitle original = subtitles.Collection[subtitleNumber]; string translatedText = translated.Text.Get(); original.Translation.Set(translatedText); } }
private void AddExtraSubtitles(SubLib.Core.Domain.Subtitles translation) { int extraCount = translation.Collection.Count - subtitles.Collection.Count; if (extraCount > 0) { subtitles.AddExtra(extraCount); } }
/* Public methods */ /// <summary>Saves subtitles to the file with the specified properties.</summary> /// <param name="subtitles">The subtitles to save.</param> /// <param name="properties">The properties of the file to save the subtitles to. Its <see cref="TimingMode" /> property is used to /// choose the timing mode for subtitle formats that support both time and frame modes.</param> /// <param name="textType">The type of text content to save.</param> /// <remarks>An updated <see cref="SubLib.FileProperties" /> object can be accessed with <see cref="FileProperties" /> after saving.</remarks> public void Save (Subtitles subtitles, FileProperties properties, SubtitleTextType textType) { SubtitleFormat format = BuiltInSubtitleFormats.GetFormat(properties.SubtitleType); SubtitleOutput output = new SubtitleOutput(format, textType); string text = output.Build(subtitles.Collection, subtitles.Properties, properties); FileInputOutput.WriteFile(properties.Path, text, properties.Encoding); fileProperties = GetUpdatedFileProperties(properties); VerboseConsole.WriteLine("[*] Saved " + textType + " \"" + properties.Path + "\" with encoding \"" + properties.Encoding + "\", format \"" + format.Name + "\" and frame rate \"" + subtitles.Properties.CurrentFrameRate + "\""); }
/// <summary>Adds the number of subtitles missing comparing to the translation.</summary> private void AddExtraSubtitles (Subtitles subtitles, Subtitles translation, int timeBetweenSubtitles) { int translationCount = translation.Collection.Count; int subtitlesCount = subtitles.Collection.Count; int extraCount = translationCount - subtitlesCount; if (extraCount <= 0) return; for (int position = subtitlesCount - 1 ; position < translationCount - 1 ; position++) { subtitles.Collection.AddNewAfter(position, subtitles.Properties, timeBetweenSubtitles); } }
private bool SaveFile(SubLib.Core.Domain.Subtitles subtitles, FileProperties properties, SubtitleTextType textType) { try { SubtitleSaver saver = new SubtitleSaver(); saver.Save(subtitles, properties, textType); return(true); } catch (Exception e) { Logger.Error(e, "Caught an exception while saving backup file"); return(false); } }
/// <summary>Auto syncs a range of subtitles given their first and last correct frames.</summary> /// <remarks>The subtitles are first shifted to the first subtitle's correct frame, and then proportionally /// adjusted using the last subtitle's correct frame.</remarks> /// <param name="subtitles">The subtitles to sync.</param> /// <param name="startIndex">The subtitle index to start the adjustment with.</param> /// <param name="startFrame">The correct start frame for the first subtitle.</param> /// <param name="endIndex">The subtitle index to end the adjustment with.</param> /// <param name="endFrame">The correct start frame for the last subtitle.</param> /// <param name="syncLast">Whether to sync the last subtitle.</param> /// <returns>Whether the subtitles could be adjusted.</returns> public static bool Sync (Subtitles subtitles, int startIndex, int startFrame, int endIndex, int endFrame, bool syncLast) { if (!AreSyncArgsValid(subtitles, startIndex, startFrame, endIndex, endFrame)) return false; /* Perform initial calculations */ int syncEndIndex = (syncLast ? endIndex : endIndex - 1); Subtitle startSubtitle = subtitles.Collection.Get(startIndex); int shift = (int)(startFrame - startSubtitle.Frames.PreciseStart); Subtitle endSubtitle = subtitles.Collection.Get(endIndex); double factor = (endFrame - startFrame) / (endSubtitle.Frames.PreciseStart - startFrame); /* Shift subtitles to the start point */ if (shift != 0) { ShiftOperator shiftOp = new ShiftOperator(subtitles); shiftOp.Shift(shift, startIndex, syncEndIndex); } /* Auto adjust timings with proportion */ for (int index = startIndex ; index <= syncEndIndex ; index++) { Subtitle subtitle = subtitles.Collection.Get(index); subtitle.Frames.Scale(factor, startFrame); } return true; }
/// <summary>Auto syncs a range of subtitles given their first and last correct times.</summary> /// <remarks>The subtitles are first shifted to the first subtitle's correct time, and then proportionally /// adjusted using the last subtitle's correct time.</remarks> /// <param name="subtitles">The subtitles to sync.</param> /// <param name="startIndex">The subtitle index to start the adjustment with.</param> /// <param name="startTime">The correct start time for the first subtitle.</param> /// <param name="endIndex">The subtitle index to end the adjustment with.</param> /// <param name="endTime">The correct start time for the last subtitle.</param> /// <param name="syncLast">Whether to sync the last subtitle.</param> /// <returns>Whether the subtitles could be adjusted.</returns> public static bool Sync (Subtitles subtitles, int startIndex, TimeSpan startTime, int endIndex, TimeSpan endTime, bool syncLast) { if (!AreSyncArgsValid(subtitles, startIndex, startTime, endIndex, endTime)) return false; /* Perform initial calculations */ int syncEndIndex = (syncLast ? endIndex : endIndex - 1); Subtitle startSubtitle = subtitles.Collection.Get(startIndex); TimeSpan shift = startTime - startSubtitle.Times.PreciseStart; Subtitle endSubtitle = subtitles.Collection.Get(endIndex); double factor = (endTime - startTime).TotalMilliseconds / (endSubtitle.Times.PreciseStart - startSubtitle.Times.PreciseStart).TotalMilliseconds; /* Shift subtitles to the start point */ if (shift != TimeSpan.Zero) { ShiftOperator shiftOp = new ShiftOperator(subtitles); shiftOp.Shift(shift, startIndex, syncEndIndex); } /* Sync timings with proportion */ for (int index = startIndex ; index <= syncEndIndex ; index++) { Subtitle subtitle = subtitles.Collection.Get(index); subtitle.Times.Scale(factor, startTime); } return true; }
/// <summary>Removes the entire translation from existing subtitles.</summary> /// <param name="subtitles">The subtitles to remove the translation from.</param> public void Clear (Subtitles subtitles) { foreach (Subtitle subtitle in subtitles.Collection) subtitle.ClearTranslation(); }
public static bool AreSyncPointsValid (Subtitles subtitles, SyncPoint start, SyncPoint end) { return AreSyncArgsValid(subtitles, start.SubtitleNumber, start.Correct.Time, end.SubtitleNumber, end.Correct.Time) && AreSyncArgsValid(subtitles, start.SubtitleNumber, start.Correct.Frame, end.SubtitleNumber, end.Correct.Frame); }
private static bool AreSyncArgsValid (Subtitles subtitles, int startIndex, int startTime, int endIndex, int endTime) { if (!AreSyncIndicesValid(subtitles, startIndex, endIndex)) return false; else if (!(startTime < endTime)) return false; else return true; }
public AdjustOperator (Subtitles subtitles) { this.subtitles = subtitles; }
private static bool AreSyncIndicesValid (Subtitles subtitles, int startIndex, int endIndex) { int subtitleCount = subtitles.Collection.Count; if (subtitleCount < 2) return false; else if (!(startIndex < endIndex)) return false; else if ((startIndex < 0) || (startIndex >= (subtitleCount - 1))) return false; else if (endIndex >= subtitleCount) return false; else return true; }
public SearchOperator (Subtitles subtitles) { this.subtitles = subtitles; }
public FrameRateOperator (Subtitles subtitles) { this.subtitles = subtitles; }
/* Public methods */ //TODO have more elaborate heuristics, taking the times into account and reporting errors /// <summary>Imports translated subtitles into existing subtitles.</summary> /// <param name="subtitles">The subtitles to import the translation to.</param> /// <param name="translation">The translated subtitles.</param> public void Import (Subtitles subtitles, Subtitles translation, int timeBetweenSubtitles) { AddExtraSubtitles(subtitles, translation, timeBetweenSubtitles); CopyTranslation(subtitles, translation); }
public static bool Sync (Subtitles subtitles, SyncPoint start, SyncPoint end, bool syncLast) { return Sync(subtitles, start.SubtitleNumber, start.Correct.Time, end.SubtitleNumber, end.Correct.Time, syncLast); }
/* Private methods */ /// <summary>Adds the number of subtitles that the translation has more than the original subtitles.</summary> /// <remarks>A gap between subtitles of <see cref="SubtitleConstants.MinTimeBetweenSubtitles" /> will be used.</remarks> private void AddExtraSubtitles (Subtitles subtitles, Subtitles translation) { AddExtraSubtitles(subtitles, translation, (int)(SubtitleConstants.MinTimeBetweenSubtitles)); }
public Subtitles(SubLib.Core.Domain.Subtitles subtitles) : base(subtitles.Collection, subtitles.Properties) { LoadModelFromCollection(); }