aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2014-06-11 15:31:33 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2014-06-11 15:31:33 -0400
commitdd7825f6c8cdd1eb30d4034d03fdaf6ff3f545be (patch)
treed9c744809eca267396cebced866d56174f8e253d /MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
parentf86e8a415af801c20f4fdf30e0b64019e9b2ad13 (diff)
Support subtitle offset
Diffstat (limited to 'MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs')
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs283
1 files changed, 0 insertions, 283 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);