// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The hand brake preset service.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.Interop.Interop
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using HandBrake.Interop.Interop.HbLib;
using HandBrake.Interop.Interop.Helpers;
using HandBrake.Interop.Interop.Interfaces.Model.Presets;
using HandBrake.Interop.Interop.Json.Presets;
using HandBrake.Interop.Utilities;
///
/// The hand brake preset service.
///
public class HandBrakePresetService
{
///
/// The get built in presets.
/// Requires an hb_init to have been invoked.
///
///
/// The .
///
public static IList GetBuiltInPresets()
{
IntPtr presets = HBFunctions.hb_presets_builtin_get_json();
string presetJson = Marshal.PtrToStringAnsi(presets);
IList presetList = JsonSerializer.Deserialize>(presetJson, JsonSettings.Options);
return presetList;
}
///
/// The get preset from file.
///
///
/// The filename.
///
///
/// The .
///
public static PresetTransportContainer GetPresetsFromFile(string filename)
{
IntPtr presetStringPointer = HBFunctions.hb_presets_read_file_json(InteropUtilities.ToUtf8PtrFromString(filename));
string presetJson = Marshal.PtrToStringAnsi(presetStringPointer);
if (!string.IsNullOrEmpty(presetJson))
{
// Check to see if we have a list of presets.
if (presetJson.StartsWith("["))
{
presetJson = "{ \"PresetList\":" + presetJson + " } ";
}
PresetTransportContainer preset = JsonSerializer.Deserialize(presetJson, JsonSettings.Options);
return preset;
}
return null;
}
///
/// The export preset.
///
///
/// The filename.
///
///
/// The container.
///
public static void ExportPreset(string filename, PresetTransportContainer container)
{
string preset = JsonSerializer.Serialize(container, JsonSettings.Options);
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write(preset);
}
}
public static PresetVersion GetCurrentPresetVersion()
{
IntPtr major = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)));
IntPtr minor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)));
IntPtr micro = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)));
HBFunctions.hb_presets_current_version(major, minor, micro);
int majorVersion = Marshal.ReadInt32(major);
int minorVersion = Marshal.ReadInt32(minor);
int microVersion = Marshal.ReadInt32(micro);
Marshal.FreeHGlobal(major);
Marshal.FreeHGlobal(minor);
Marshal.FreeHGlobal(micro);
return new PresetVersion(majorVersion, minorVersion, microVersion);
}
}
}