让手刹编码器工作

Jos*_*osh 4 c# exception video-encoding handbrake

我无法让Handbrake在C#中为我工作.它不断抛出错误"Value not not null"..正如您在下面的屏幕截图中看到的,我根据我所知道的尝试了几件事.我很感激你的任何指导,帮助我完成这项工作.

手刹编码器错误

到目前为止我尝试了什么(点数与显示的错误无关.仅供参考):

  1. 在我的电脑上安装手刹
  2. 添加了对HandBrakeInterop.dll的引用
  3. 将所有其他必需的dll复制到我的调试文件夹,甚至尝试将它们添加为引用
  4. 尝试从x86/x64更改调试版本

我哪里不在正确的轨道上?

这是我的代码:

      HandBrake.Interop.HandBrakeInstance objHb = new HandBrake.Interop.HandBrakeInstance();
    HandBrake.Interop.Model.EncodeJob objJob = new HandBrake.Interop.Model.EncodeJob();
    objJob.SourceType = HandBrake.Interop.Model.SourceType.File;
    EncodingProfile objProfile = new EncodingProfile();
    objProfile.OutputFormat = Container.Mp4;
    objProfile.VideoEncodeRateType = VideoEncodeRateType.ConstantQuality;
    objProfile.IPod5GSupport = true;
    objProfile.PreferredExtension = OutputExtension.Mp4;
   // objJob.EncodingProfile = objProfile;
    objJob.SourcePath = AppDomain.CurrentDomain.BaseDirectory + "1.flv";
    objJob.OutputPath = AppDomain.CurrentDomain.BaseDirectory + "1.mp4";
    //objHb.Titles.Add(new HandBrake.Interop.SourceData.Title());

    XmlSerializer objSerializer = new XmlSerializer(typeof(EncodingProfile));


   MemoryStream mem=new MemoryStream(File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "normal.xml"));
 var objPresetFromFile =  objSerializer.Deserialize(mem) as EncodingProfile;
 objJob.EncodingProfile = objPresetFromFile;
    objJob.UseDefaultChapterNames = true;
   // objJob.Title = 1;



    //HandBrake.Interop.Model.Encoders
    objHb.EncodeProgress += objHb_EncodeProgress;
    objHb.EncodeCompleted += objHb_EncodeCompleted;
    HandBrake.ApplicationServices.Parsing.Title title = new HandBrake.ApplicationServices.Parsing.Title();// new HandBrake.Interop.SourceData.Title();
    objHb.Initialize(5);
    //objHb.Titles = new System.Collections.Generic.List<HandBrake.Interop.SourceData.Title>();
    objHb.StartScan(AppDomain.CurrentDomain.BaseDirectory + "1.flv", 0);
    objJob.SourceType = HandBrake.Interop.Model.SourceType.File;
    objJob.RangeType = HandBrake.Interop.Model.VideoRangeType.Frames;

    objHb.StartEncode(objJob);

    Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

编辑:根据要求,这里有更多信息从异常复制到剪贴板

System.ArgumentNullException was unhandled
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: source
  Source=System.Core
  ParamName=source
  StackTrace:
       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
       at HandBrake.Interop.HandBrakeInstance.GetTitle(Int32 titleNumber) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 1604
       at HandBrake.Interop.HandBrakeInstance.GetTitleIndex(Int32 titleNumber) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 1614
       at HandBrake.Interop.HandBrakeInstance.StartEncode(EncodeJob job, Boolean preview, Int32 previewNumber, Int32 previewSeconds, Double overallSelectedLengthSeconds) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 411
       at HandBrake.Interop.HandBrakeInstance.StartEncode(EncodeJob jobToStart) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 395
       at HandBrakeTest.Program.Main(String[] args) in c:\Users\user\Documents\Visual Studio 2012\Projects\HandBrakeTest\HandBrakeTest\Program.cs:line 53
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

编辑:

  1. 将handbrakeinstance和encodingjob的实例移到main之外并使它们变为静态
  2. 在扫描完成事件中调用StartEncode

现在,Nullreferenceexception的例外是详细信息:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=HandBrakeInterop
  StackTrace:
       at HandBrake.Interop.InteropUtilities.ReadStructure[T](IntPtr structPtr) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\InteropUtilities.cs:line 31
       at HandBrake.Interop.HandBrakeInstance.StartEncode(EncodeJob job, Boolean preview, Int32 previewNumber, Int32 previewSeconds, Double overallSelectedLengthSeconds) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 412
       at HandBrake.Interop.HandBrakeInstance.StartEncode(EncodeJob jobToStart) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 395
       at HandBrakeTest.Program.objHb_ScanCompleted(Object sender, EventArgs e) in c:\Users\user\Documents\Visual Studio 2012\Projects\HandBrakeTest\HandBrakeTest\Program.cs:line 65
       at HandBrake.Interop.HandBrakeInstance.PollScanProgress() in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 811
       at HandBrake.Interop.HandBrakeInstance.<StartScan>b__2(Object o, ElapsedEventArgs e) in c:\HandBrake\Hudson\workspace\Release_WindowsGui64\0.9.9\win\CS\HandBrake.Interop\HandBrakeInterop\HandBrakeInstance.cs:line 752
       at System.Timers.Timer.MyTimerCallback(Object state)
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Ran*_*ngy 6

是的,HandBrakeInterop不是最容易编写代码的API; 它现在更适合全编码器GUI; 它不会很好地回归默认值.从XML中获取EncodingProfile你有一个非常好的主意.

这是一个与HandBrakeInterop交互的工作最小项目:http: //engy.us/misc/HandBrakeInteropExample.zip

一些相关的位:

instance = new HandBrakeInstance();
instance.Initialize(verbosity: 1);
instance.ScanCompleted += instance_ScanCompleted;
instance.StartScan(SourceFile, previewCount: 10);
Run Code Online (Sandbox Code Playgroud)
  • 1是默认的详细程度.您传入的"5"无效.
  • 您可能希望在StartScan上传递previewCount的实际值:它使用这些静态预览来执行自动裁剪检测和梳理检测等操作.
var job = new EncodeJob
    {
        EncodingProfile = profile,
        RangeType = VideoRangeType.All,
        Title = 1,
        SourcePath = SourceFile,
        OutputPath = AppDomain.CurrentDomain.BaseDirectory + "Output.mp4",
        ChosenAudioTracks = new List { 1 },
        Subtitles = new Subtitles
            {
                SourceSubtitles = new List(),
                SrtSubtitles = new List()
            }
    };
  • 您需要传入要编码的标题(基于1的索引).对于文件,这很少相关; 该功能专为DVD /蓝光扫描而设计.
  • 您必须传入RangeType范围的a 和细节:例如,如果您选择a RangeType,Frames则需要指定FramesStartFramesEnd.RangeType.All这是最简单的,因为您不需要指定任何额外的东西.
  • 您需要指定所选音轨索引的列表(从1开始).通常不适用于文件,但适用于具有多种语言的DVD /蓝光光盘.
  • 现在你必须在Subtitles下给它一些东西,否则它会崩溃.这实际上是一个bug; 有几个案例我没有正确守护.我将来会解决这个问题.

如果您想查看API的完整参考实现,请查看VidCoder.HandBrake已开始使用它,但它还没有涉及静态预览和暂停/恢复等所有功能.

更新:现在所需的代码看起来完全不同,目标是HandBrake.ApplicationServices.dll.新的最小项目:http://engy.us/misc/HBInteropExampleV3.zip

一个显着的区别是编码作业现在以JSON指定.这是在HandBrake/VidCoder的编码日志中吐出的完全相同的JSON blob,因此您可以从那里窃取它并进行调整.