From 2c5f96a2fdbe7407fd360e424ac43cac0fd3ad26 Mon Sep 17 00:00:00 2001 From: RandomEngy Date: Thu, 16 Nov 2017 23:02:28 -0800 Subject: [PATCH] Some additions for mixdowns and ability encode from JSON string. --- .../Interop/HandBrakeEncoderHelpers.cs | 21 ++++++++++++ .../Interop/HandBrakeInstance.cs | 34 ++++++++++++++++--- .../Interop/HbLib/HbFunctions.cs | 2 ++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeEncoderHelpers.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeEncoderHelpers.cs index b0e72976abc4..125c04449097 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeEncoderHelpers.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeEncoderHelpers.cs @@ -244,6 +244,16 @@ public static HBMixdown GetMixdown(string shortName) return Mixdowns.SingleOrDefault(m => m.ShortName == shortName); } + /// + /// Gets the mixdown with the specified ID. + /// + /// The mixdown ID. + /// The requested mixdown. + public static HBMixdown GetMixdown(int id) + { + return Mixdowns.SingleOrDefault(m => m.Id == id); + } + /// /// Gets the container with the specified short name. /// @@ -489,6 +499,17 @@ public static HBMixdown GetDefaultMixdown(HBAudioEncoder encoder, ulong layout) return Mixdowns.Single(m => m.Id == defaultMixdown); } + /// + /// Sanitizes the given sample rate for the given encoder. + /// + /// The encoder. + /// The sample rate to sanitize. + /// The sanitized sample rate. + public static int SanitizeSampleRate(HBAudioEncoder encoder, int sampleRate) + { + return HBFunctions.hb_audio_samplerate_find_closest(sampleRate, (uint)encoder.Id); + } + /// /// Gets the bitrate limits for the given audio codec, sample rate and mixdown. /// diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs index 45b2f84882aa..b44dd385c7ae 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs @@ -82,6 +82,11 @@ public class HandBrakeInstance : IHandBrakeInstance, IDisposable /// private JsonScanObject titles; + /// + /// The raw JSON for the titles list. + /// + private string titlesJson; + /// /// The index of the default title. /// @@ -153,6 +158,17 @@ public JsonScanObject Titles } } + /// + /// Gets the raw JSON for the list of titles on this instance. + /// + public string TitlesJson + { + get + { + return this.titlesJson; + } + } + /// /// Gets the index of the default title. /// @@ -360,7 +376,17 @@ public void StartEncode(JsonEncodeObject encodeObject) }; string encode = JsonConvert.SerializeObject(encodeObject, Formatting.Indented, settings); - HBFunctions.hb_add_json(this.hbHandle, InteropUtilities.ToUtf8PtrFromString(encode)); + this.StartEncode(encode); + } + + /// + /// Starts an encode with the given job JSON. + /// + /// The JSON for the job to start. + [HandleProcessCorruptedStateExceptions] + public void StartEncode(string encodeJson) + { + HBFunctions.hb_add_json(this.hbHandle, InteropUtilities.ToUtf8PtrFromString(encodeJson)); HBFunctions.hb_start(this.hbHandle); this.encodePollTimer = new Timer(); @@ -497,8 +523,8 @@ private void PollScanProgress() this.scanPollTimer.Stop(); var jsonMsg = HBFunctions.hb_get_title_set_json(this.hbHandle); - string scanJson = InteropUtilities.ToStringFromUtf8Ptr(jsonMsg); - this.log.LogMessage(scanJson, LogMessageType.Progress, LogLevel.Trace); + this.titlesJson = InteropUtilities.ToStringFromUtf8Ptr(jsonMsg); + this.log.LogMessage(this.titlesJson, LogMessageType.Progress, LogLevel.Trace); if (string.IsNullOrEmpty(scanJson)) { @@ -506,7 +532,7 @@ private void PollScanProgress() } else { - this.titles = JsonConvert.DeserializeObject(scanJson); + this.titles = JsonConvert.DeserializeObject(this.titlesJson); if (this.titles != null) { this.featureTitle = this.titles.MainFeature; diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs b/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs index beaf2e689672..63eac34684c3 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HbLib/HbFunctions.cs @@ -201,6 +201,8 @@ public static class HBFunctions [DllImport("hb", EntryPoint = "hb_audio_samplerate_get_next", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr hb_audio_samplerate_get_next(IntPtr last); + [DllImport("hb", EntryPoint = "hb_audio_samplerate_find_closest", CallingConvention = CallingConvention.Cdecl)] + public static extern int hb_audio_samplerate_find_closest(int samplerate, uint codec); [DllImport("hb", EntryPoint = "hb_audio_bitrate_get_best", CallingConvention = CallingConvention.Cdecl)] public static extern int hb_audio_bitrate_get_best(uint codec, int bitrate, int samplerate, int mixdown);