我无法让Handbrake在 C# 中为我工作。它不断抛出错误“值不能为空”。. 正如您在下面的屏幕截图中看到的那样,我根据我所知道的尝试了几件事。我将不胜感激您的任何指导,以帮助我完成这项工作。
到目前为止我所尝试的(点与显示的错误无关..仅供参考):
- 在我的电脑上安装手刹
- 添加了对 HandBrakeInterop.dll 的引用
- 将所有其他必需的 dll 复制到我的调试文件夹中,甚至尝试将它们添加为引用
- 尝试从 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();
编辑:根据要求,这里是从异常复制到剪贴板的更多信息
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:
编辑 :
- 将 handbrakeinstance 和 encodingjob 的实例移到 main 之外并使其成为静态
- 在扫描完成事件中调用 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:
提前致谢。