aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.MediaEncoding')
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs283
-rw-r--r--MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs1
-rw-r--r--MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs134
3 files changed, 130 insertions, 288 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
index c14e7d476..d532d100f 100644
--- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
@@ -285,289 +285,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
return string.Empty;
}
- /// <summary>
- /// Gets the subtitle language encoding param.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="language">The language.</param>
- /// <returns>System.String.</returns>
- public string GetSubtitleLanguageEncodingParam(string path, string language)
- {
- if (GetFileEncoding(path).Equals(Encoding.UTF8))
- {
- return string.Empty;
- }
-
- switch (language.ToLower())
- {
- case "pol":
- case "cze":
- case "ces":
- case "slo":
- case "slk":
- case "hun":
- case "slv":
- case "srp":
- case "hrv":
- case "rum":
- case "ron":
- case "rup":
- case "alb":
- case "sqi":
- return "windows-1250";
- case "ara":
- return "windows-1256";
- case "heb":
- return "windows-1255";
- case "grc":
- case "gre":
- return "windows-1253";
- case "crh":
- case "ota":
- case "tur":
- return "windows-1254";
- case "rus":
- return "windows-1251";
- case "vie":
- return "windows-1258";
- case "kor":
- return "cp949";
- default:
- return "windows-1252";
- }
- }
-
- private static Encoding GetFileEncoding(string srcFile)
- {
- // *** Detect byte order mark if any - otherwise assume default
- var buffer = new byte[5];
-
- using (var file = new FileStream(srcFile, FileMode.Open))
- {
- file.Read(buffer, 0, 5);
- }
-
- if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
- return Encoding.UTF8;
- if (buffer[0] == 0xfe && buffer[1] == 0xff)
- return Encoding.Unicode;
- if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
- return Encoding.UTF32;
- if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
- return Encoding.UTF7;
-
- // It's ok - anything aside from utf is ok since that's what we're looking for
- return Encoding.Default;
- }
-
- /// <summary>
- /// Extracts the text subtitle.
- /// </summary>
- /// <param name="inputFiles">The input files.</param>
- /// <param name="type">The type.</param>
- /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
- /// <param name="copySubtitleStream">if set to true, copy stream instead of converting.</param>
- /// <param name="outputPath">The output path.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task.</returns>
- /// <exception cref="System.ArgumentException">Must use inputPath list overload</exception>
- public async Task ExtractTextSubtitle(string[] inputFiles, InputType type, int subtitleStreamIndex,
- bool copySubtitleStream, string outputPath, CancellationToken cancellationToken)
- {
- var semaphore = GetLock(outputPath);
-
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
-
- try
- {
- if (!File.Exists(outputPath))
- {
- await
- ExtractTextSubtitleInternal(GetInputArgument(inputFiles, type), subtitleStreamIndex,
- copySubtitleStream, outputPath, cancellationToken).ConfigureAwait(false);
- }
- }
- finally
- {
- semaphore.Release();
- }
- }
-
- /// <summary>
- /// Extracts the text subtitle.
- /// </summary>
- /// <param name="inputPath">The input path.</param>
- /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
- /// <param name="copySubtitleStream">if set to true, copy stream instead of converting.</param>
- /// <param name="outputPath">The output path.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task.</returns>
- /// <exception cref="System.ArgumentNullException">inputPath
- /// or
- /// outputPath
- /// or
- /// cancellationToken</exception>
- /// <exception cref="System.ApplicationException"></exception>
- private async Task ExtractTextSubtitleInternal(string inputPath, int subtitleStreamIndex,
- bool copySubtitleStream, string outputPath, CancellationToken cancellationToken)
- {
- if (string.IsNullOrEmpty(inputPath))
- {
- throw new ArgumentNullException("inputPath");
- }
-
- if (string.IsNullOrEmpty(outputPath))
- {
- throw new ArgumentNullException("outputPath");
- }
-
- string processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s ass \"{2}\"", inputPath,
- subtitleStreamIndex, outputPath);
-
- if (copySubtitleStream)
- {
- processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s copy \"{2}\"", inputPath,
- subtitleStreamIndex, outputPath);
- }
-
- var process = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- CreateNoWindow = true,
- UseShellExecute = false,
-
- RedirectStandardOutput = false,
- RedirectStandardError = true,
-
- FileName = FFMpegPath,
- Arguments = processArgs,
- WindowStyle = ProcessWindowStyle.Hidden,
- ErrorDialog = false
- }
- };
-
- _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
-
- var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "ffmpeg-sub-extract-" + Guid.NewGuid() + ".txt");
- Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
-
- var logFileStream = _fileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read,
- true);
-
- try
- {
- process.Start();
- }
- catch (Exception ex)
- {
- logFileStream.Dispose();
-
- _logger.ErrorException("Error starting ffmpeg", ex);
-
- throw;
- }
-
- process.StandardError.BaseStream.CopyToAsync(logFileStream);
-
- var ranToCompletion = process.WaitForExit(60000);
-
- if (!ranToCompletion)
- {
- try
- {
- _logger.Info("Killing ffmpeg subtitle extraction process");
-
- process.Kill();
-
- process.WaitForExit(1000);
- }
- catch (Exception ex)
- {
- _logger.ErrorException("Error killing subtitle extraction process", ex);
- }
- finally
- {
- logFileStream.Dispose();
- }
- }
-
- var exitCode = ranToCompletion ? process.ExitCode : -1;
-
- process.Dispose();
-
- var failed = false;
-
- if (exitCode == -1)
- {
- failed = true;
-
- if (File.Exists(outputPath))
- {
- try
- {
- _logger.Info("Deleting extracted subtitle due to failure: ", outputPath);
- File.Delete(outputPath);
- }
- catch (IOException ex)
- {
- _logger.ErrorException("Error deleting extracted subtitle {0}", ex, outputPath);
- }
- }
- }
- else if (!File.Exists(outputPath))
- {
- failed = true;
- }
-
- if (failed)
- {
- var msg = string.Format("ffmpeg subtitle extraction failed for {0} to {1}", inputPath, outputPath);
-
- _logger.Error(msg);
-
- throw new ApplicationException(msg);
- }
- else
- {
- var msg = string.Format("ffmpeg subtitle extraction completed for {0} to {1}", inputPath, outputPath);
-
- _logger.Info(msg);
- }
-
- await SetAssFont(outputPath).ConfigureAwait(false);
- }
-
- /// <summary>
- /// Sets the ass font.
- /// </summary>
- /// <param name="file">The file.</param>
- /// <returns>Task.</returns>
- private async Task SetAssFont(string file)
- {
- _logger.Info("Setting ass font within {0}", file);
-
- string text;
- Encoding encoding;
-
- using (var reader = new StreamReader(file, detectEncodingFromByteOrderMarks: true))
- {
- encoding = reader.CurrentEncoding;
-
- text = await reader.ReadToEndAsync().ConfigureAwait(false);
- }
-
- var newText = text.Replace(",Arial,", ",Arial Unicode MS,");
-
- if (!string.Equals(text, newText))
- {
- using (var writer = new StreamWriter(file, false, encoding))
- {
- writer.Write(newText);
- }
- }
- }
-
public Task<Stream> ExtractAudioImage(string path, CancellationToken cancellationToken)
{
return ExtractImage(new[] { path }, InputType.File, true, null, null, cancellationToken);
diff --git a/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs b/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs
index 80fd0d602..0f1f17bc0 100644
--- a/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs
+++ b/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
+using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs
index 7b783711a..461f95174 100644
--- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs
@@ -47,13 +47,16 @@ namespace MediaBrowser.MediaEncoding.Subtitles
public async Task<Stream> ConvertSubtitles(Stream stream,
string inputFormat,
string outputFormat,
+ long startTimeTicks,
CancellationToken cancellationToken)
{
var ms = new MemoryStream();
try
{
- if (string.Equals(inputFormat, outputFormat, StringComparison.OrdinalIgnoreCase))
+ // Return the original without any conversions, if possible
+ if (startTimeTicks == 0 &&
+ string.Equals(inputFormat, outputFormat, StringComparison.OrdinalIgnoreCase))
{
await stream.CopyToAsync(ms, 81920, cancellationToken).ConfigureAwait(false);
}
@@ -61,6 +64,8 @@ namespace MediaBrowser.MediaEncoding.Subtitles
{
var trackInfo = await GetTrackInfo(stream, inputFormat, cancellationToken).ConfigureAwait(false);
+ UpdateStartingPosition(trackInfo, startTimeTicks);
+
var writer = GetWriter(outputFormat);
writer.Write(trackInfo, ms, cancellationToken);
@@ -76,10 +81,26 @@ namespace MediaBrowser.MediaEncoding.Subtitles
return ms;
}
+ private void UpdateStartingPosition(SubtitleTrackInfo track, long startPositionTicks)
+ {
+ if (startPositionTicks == 0) return;
+
+ foreach (var trackEvent in track.TrackEvents)
+ {
+ trackEvent.EndPositionTicks -= startPositionTicks;
+ trackEvent.StartPositionTicks -= startPositionTicks;
+ }
+
+ track.TrackEvents = track.TrackEvents
+ .SkipWhile(i => i.StartPositionTicks < 0 || i.EndPositionTicks < 0)
+ .ToList();
+ }
+
public async Task<Stream> GetSubtitles(string itemId,
string mediaSourceId,
int subtitleStreamIndex,
string outputFormat,
+ long startTimeTicks,
CancellationToken cancellationToken)
{
var subtitle = await GetSubtitleStream(itemId, mediaSourceId, subtitleStreamIndex, cancellationToken)
@@ -89,7 +110,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
{
var inputFormat = subtitle.Item2;
- return await ConvertSubtitles(stream, inputFormat, outputFormat, cancellationToken).ConfigureAwait(false);
+ return await ConvertSubtitles(stream, inputFormat, outputFormat, startTimeTicks, cancellationToken).ConfigureAwait(false);
}
}
@@ -127,11 +148,36 @@ namespace MediaBrowser.MediaEncoding.Subtitles
var fileInfo = await GetReadableFile(mediaSource.Path, inputFiles, inputType, subtitleStream, cancellationToken).ConfigureAwait(false);
- var stream = File.OpenRead(fileInfo.Item1);
+ var stream = await GetSubtitleStream(fileInfo.Item1, subtitleStream.Language).ConfigureAwait(false);
return new Tuple<Stream, string>(stream, fileInfo.Item2);
}
+ private async Task<Stream> GetSubtitleStream(string path, string language)
+ {
+ if (!string.IsNullOrEmpty(language))
+ {
+ var charset = GetSubtitleFileCharacterSet(path, language);
+
+ if (!string.IsNullOrEmpty(charset))
+ {
+ using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
+ {
+ using (var reader = new StreamReader(fs, Encoding.GetEncoding(charset)))
+ {
+ var text = await reader.ReadToEndAsync().ConfigureAwait(false);
+
+ var bytes = Encoding.UTF8.GetBytes(text);
+
+ return new MemoryStream(bytes);
+ }
+ }
+ }
+ }
+
+ return File.OpenRead(path);
+ }
+
private async Task<Tuple<string, string>> GetReadableFile(string mediaPath,
string[] inputFiles,
InputType type,
@@ -282,10 +328,11 @@ namespace MediaBrowser.MediaEncoding.Subtitles
throw new ArgumentNullException("outputPath");
}
+ Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
var encodingParam = string.IsNullOrEmpty(language)
? string.Empty
- : _mediaEncoder.GetSubtitleLanguageEncodingParam(inputPath, language);
+ : GetSubtitleFileCharacterSet(inputPath, language);
if (!string.IsNullOrEmpty(encodingParam))
{
@@ -456,7 +503,9 @@ namespace MediaBrowser.MediaEncoding.Subtitles
throw new ArgumentNullException("outputPath");
}
- string processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s ass \"{2}\"", inputPath,
+ Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
+
+ var processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s ass \"{2}\"", inputPath,
subtitleStreamIndex, outputPath);
if (copySubtitleStream)
@@ -615,5 +664,80 @@ namespace MediaBrowser.MediaEncoding.Subtitles
return Path.Combine(SubtitleCachePath, prefix, filename);
}
+
+ /// <summary>
+ /// Gets the subtitle language encoding param.
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="language">The language.</param>
+ /// <returns>System.String.</returns>
+ public string GetSubtitleFileCharacterSet(string path, string language)
+ {
+ if (GetFileEncoding(path).Equals(Encoding.UTF8))
+ {
+ return string.Empty;
+ }
+
+ switch (language.ToLower())
+ {
+ case "pol":
+ case "cze":
+ case "ces":
+ case "slo":
+ case "slk":
+ case "hun":
+ case "slv":
+ case "srp":
+ case "hrv":
+ case "rum":
+ case "ron":
+ case "rup":
+ case "alb":
+ case "sqi":
+ return "windows-1250";
+ case "ara":
+ return "windows-1256";
+ case "heb":
+ return "windows-1255";
+ case "grc":
+ case "gre":
+ return "windows-1253";
+ case "crh":
+ case "ota":
+ case "tur":
+ return "windows-1254";
+ case "rus":
+ return "windows-1251";
+ case "vie":
+ return "windows-1258";
+ case "kor":
+ return "cp949";
+ default:
+ return "windows-1252";
+ }
+ }
+
+ private static Encoding GetFileEncoding(string srcFile)
+ {
+ // *** Detect byte order mark if any - otherwise assume default
+ var buffer = new byte[5];
+
+ using (var file = new FileStream(srcFile, FileMode.Open))
+ {
+ file.Read(buffer, 0, 5);
+ }
+
+ if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
+ return Encoding.UTF8;
+ if (buffer[0] == 0xfe && buffer[1] == 0xff)
+ return Encoding.Unicode;
+ if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
+ return Encoding.UTF32;
+ if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
+ return Encoding.UTF7;
+
+ // It's ok - anything aside from utf is ok since that's what we're looking for
+ return Encoding.Default;
+ }
}
}