Tutorial / "How-to" for Handbrake .NET

HandBrake for Windows support
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
elvinny
Posts: 2
Joined: Tue Jul 26, 2011 3:52 pm

Tutorial / "How-to" for Handbrake .NET

Post by elvinny »

Hi


There is any documentation for HandBrake .NET integration. I would like to integrate and use Handbrake but i can't find any tutorial or simple documentation like "how-to" encode, stop encoding or using Queue encode with Handbrake.


Regards


Luis
RandomEngy
Novice
Posts: 50
Joined: Thu Mar 29, 2007 2:56 am

Re: Tutorial / "How-to" for Handbrake .NET

Post by RandomEngy »

Are you trying to use the HandBrake engine or are you trying to manipulate the HandBrake GUI with .NET calls? (In either case I do not believe there are any documentation about it)
elvinny
Posts: 2
Joined: Tue Jul 26, 2011 3:52 pm

Re: Tutorial / "How-to" for Handbrake .NET

Post by elvinny »

Using the Handbrake engine to produce a farm encoding.
RandomEngy
Novice
Posts: 50
Joined: Thu Mar 29, 2007 2:56 am

Re: Tutorial / "How-to" for Handbrake .NET

Post by RandomEngy »

I'm not sure what you mean by "farm encoding" but I think what you're looking for is HandBrakeInterop.dll. It's checked in to the HandBrake source as its own standalone solution. If you have that DLL and hb.dll (from the appropriate build) you can call into it and do scans, preview creation and encodes. That's what I use in VidCoder. And you should be sure that your HandBrakeInterop.dll and hb.dll match up in terms of version. They often change the structures and I have to tweak HandBrakeInterop to keep up. The VidCoder source will usually have a matched up pair in the Lib folder.

Anyway the best reference point is just the XML comments in the HandBrakeInterop source.

Roughly what you need to do is:

Create a HandBrakeInterop object
Initialize it
Run a scan on a source
Pass in an EncodeJob object to run an encode.

The EncodeJob object has a lot of junk on it that you'll need to set, including all the picture settings, filters, video encoder, quality, audio tracks, everything. There isn't a "default" like there is with the CLI because it's meant to be used by a full-on GUI editor that has all of those values. Though you should be able to run a scan without too much code.
Taddeusz
Posts: 18
Joined: Mon Jan 07, 2008 10:40 pm

Re: Tutorial / "How-to" for Handbrake .NET

Post by Taddeusz »

I'm trying to convert a program I've written in VB.NET from using the HandBrake CLI to using handbrakeinterop.dll. I realize VB.NET probably isn't the best way to use it but that's what I know until I can wrap my head around C#. Are there any guides to integrating HandBrakeInterop?

My main issue right now is that when I pass the EncodeJob to StartEncode of my instance I get a null object reference. What could I be missing? Keep in mind this is just test code. I'll clean it up and modularize it once I have it all worked out. The relevant code from my project is as follows:

Code: Select all

Dim hbscanprog As New ScanProgressEventArgs
                Dim HBEncode As New EncodeJob
                Dim HBEncodeProfile As New Encoding.EncodingProfile
                Dim HBAudio As New AudioEncoding
                Dim HBAudioList As New List(Of AudioEncoding)
                Dim HBAudioTracks As New List(Of Integer)

                Using HBInst As New HandBrakeInstance
                    HBInst.Initialize(0)

                    HBInst.StartScan(lstVideos.Items(0).ToString, 1, 0)

                    HBAudio.Encoder = 1
                    HBAudio.EncodeRateType = AudioEncodeRateType.Bitrate
                    HBAudio.Bitrate = 128
                    HBAudio.SampleRateRaw = 48000
                    HBAudio.Mixdown = "Dolby Pro Logic II"
                    HBAudio.InputNumber = 1

                    HBAudioList.Add(HBAudio)

                    HBEncodeProfile.OutputFormat = Encoding.Container.Mp4
                    HBEncodeProfile.AudioEncodings = HBAudioList
                    HBEncodeProfile.Anamorphic = Encoding.Anamorphic.None
                    HBEncodeProfile.Width = 480
                    HBEncodeProfile.MaxHeight = 320
                    HBEncodeProfile.Modulus = 16
                    HBEncodeProfile.VideoEncodeRateType = VideoEncodeRateType.ConstantQuality
                    HBEncodeProfile.Quality = 20
                    HBEncodeProfile.VideoEncoder = "x264"
                    HBEncodeProfile.X264Options = "cabac=0:ref=2:me=umh:bframes=0:weightp=0:subq=6:8x8dct=0:trellis=0"
                    HBEncodeProfile.IncludeChapterMarkers = True

                    HBEncode.EncodingProfile = HBEncodeProfile
                    HBEncode.SourceType = SourceType.File
                    HBEncode.SourcePath = lstVideos.Items(0).ToString
                    HBEncode.Title = 1
                    HBEncode.Angle = 0
                    HBEncode.OutputPath = OutPath & "\" & Outfile & ".m4v"
                    HBAudioTracks.Add(1)
                    HBEncode.ChosenAudioTracks = HBAudioTracks

                    HBInst.StartEncode(HBEncode)
                End Using
RandomEngy
Novice
Posts: 50
Joined: Thu Mar 29, 2007 2:56 am

Re: Tutorial / "How-to" for Handbrake .NET

Post by RandomEngy »

Can you post the stack trace from the exception?
User avatar
s55
HandBrake Team
Posts: 10358
Joined: Sun Dec 24, 2006 1:05 pm

Re: Tutorial / "How-to" for Handbrake .NET

Post by s55 »

The Interop isn't up to date with API changes, so won't work for encodes, only scans. I started updating for Johns API changes but I think there was a pending review blocking me from proceeding so I sidelined it. I can't remember if his changs ever got checked in. Either way, the interop lib is in need of some TLC.

If I have time, I'll take a look at it at the weekend see where things are at.
Taddeusz
Posts: 18
Joined: Mon Jan 07, 2008 10:40 pm

Re: Tutorial / "How-to" for Handbrake .NET

Post by Taddeusz »

Here is the stack trace:

Code: Select all

System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: source
  ParamName=source
  Source=System.Core
  StackTrace:
       at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
       at HandBrake.Interop.HandBrakeInstance.GetOriginalTitle(Int32 titleIndex)
       at HandBrake.Interop.HandBrakeInstance.StartEncode(EncodeJob job, Boolean preview, Int32 previewNumber, Int32 previewSeconds, Double overallSelectedLengthSeconds)
       at HandBrake.Interop.HandBrakeInstance.StartEncode(EncodeJob jobToStart)
       at Batch_Video_Converter.frmBatchVideo.btnEncode_Click(Object sender, EventArgs e) in C:\Users\Taddeusz\Documents\Visual Studio 2010\Projects\batch-video-converter\Batch Video Converter\frmBatchVideo.vb:line 620
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Batch_Video_Converter.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Taddeusz
Posts: 18
Joined: Mon Jan 07, 2008 10:40 pm

Re: Tutorial / "How-to" for Handbrake .NET

Post by Taddeusz »

Alright, well. My desire to try and use the API is due to a problem I had began having with using the CLI. I think it's probably errors in the ts files from my Hauppauge HD-PVR though. Essentially what I am doing is taking recordings from my HD-PVR, using comskip or Show Analyzer to detect ads, and then I'm using the program I wrote to transcode only the sections of video that aren't commercials. I use the values in the EDL file to determine start and stop points/lengths. I think what is happening is that sometimes there is some kind of error and the CLI while scanning for the beginning of the video it fails to find it. Here is the output of running the following command:

Code: Select all

C:\Program Files\Handbrake>HandBrakeCLI.exe --crop 0:0:0:0 -Z "iPhone & iPod Touch" -i "W:\TheGlades-OldTimes-12703388-0.ts" -o "C:\temp\TheGlades-OldTimes-12703388-0.2.m4v" --start-at duration:1048.18 --stop-at duration:630.98

Code: Select all

[10:29:36] hb_init: starting libhb thread
HandBrake 0.9.6 (2012022800) - MinGW x86_64 - http://handbrake.fr
4 CPUs detected
Opening W:\TheGlades-OldTimes-12703388-0.ts...
[10:29:36] hb_scan: path=W:\TheGlades-OldTimes-12703388-0.ts, title_index=1
libbluray/bdnav/index_parse.c:157: indx_parse(): error opening W:\TheGlades-OldT
imes-12703388-0.ts/BDMV/index.bdmv
libbluray/bluray.c:1471: nav_get_title_list(W:\TheGlades-OldTimes-12703388-0.ts)
 failed (000000000032D870)
[10:29:36] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 4.1.3
libdvdread: Encrypted DVD support unavailable.
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[10:29:36] dvd: not a dvd - trying as a stream/file instead
[10:29:36] file is MPEG Transport Stream with 188 byte packets offset 0 bytes
[10:29:36] Found the following PIDS
[10:29:36]     Video PIDS :
[10:29:36]       0x1011 type H.264 (0x1b)
[10:29:36]     Audio PIDS :
[10:29:36]       0x1100 type AC3 (0x81)
[10:29:36]     Other PIDS :
[10:29:36]       0x1001 type Unknown (0xff) (PCR)
[10:29:36] stream id 0x1100 (type 0x81 substream 0x0) audio 0x1100
[10:29:37] scan: decoding previews for title 1
[10:29:37] scan: audio 0x1100: AC-3, rate=48000Hz, bitrate=384000 Unknown (AC3)
(5.1 ch)
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
Scanning title 1...
[10:29:44] scan: 10 previews, 1280x720, 59.940 fps, autocrop = 8/0/0/2, aspect 1
6:9, PAR 1:1
[10:29:44] scan: title (0) job->width:1264, job->height:704
[10:29:44] stream: 60 good frames, 0 errors (0%)
[10:29:44] libhb: scan thread found 1 valid title(s)
+ title 1:
  + stream: W:\TheGlades-OldTimes-12703388-0.ts
  + duration: 00:59:35
  + size: 1280x720, pixel aspect: 1/1, display aspect: 1.78, 59.940 fps
  + autocrop: 8/0/0/2
  + chapters:
    + 1: cells 0->0, 0 blocks, duration 00:59:35
  + audio tracks:
    + 1, Unknown (AC3) (5.1 ch) (iso639-2: und), 48000Hz, 384000bps
  + subtitle tracks:
+ Using preset: iPhone & iPod Touch
[10:29:44] 1 job(s) to process
[10:29:44] starting job
[10:29:44] Width out of bounds, scaling down to 480
[10:29:44] New dimensions 480 * 272
[10:29:44] sync: expecting 37822 video frames
[10:29:44] work: only 1 chapter, disabling chapter markers
[10:29:44] job configuration:
[10:29:44]  * source
[10:29:44]    + W:\TheGlades-OldTimes-12703388-0.ts
[10:29:44]    + title 1, start 0:17:28.00 stop 0:27:58.00
[10:29:44]  * destination
[10:29:44]    + C:\temp\TheGlades-OldTimes-12703388-0.2.m4v
[10:29:44]    + container: MPEG-4 (.mp4 and .m4v)
[10:29:44]  * video track
[10:29:44]    + decoder: h264
[10:29:44]      + bitrate 200 kbps
[10:29:44]    + frame rate: same as source (around 59.940 fps)
[10:29:44]    + dimensions: 1280 * 720 -> 480 * 272, crop 0/0/0/0, mod 0
[10:29:44]    + encoder: H.264 (x264)
[10:29:44]      + options: cabac=0:ref=2:me=umh:bframes=0:weightp=0:subme=6:8x8d
ct=0:trellis=0
[10:29:44]      + quality: 20.00 (RF)
[10:29:44]  * audio track 1
[10:29:44]    + decoder: Unknown (AC3) (5.1 ch) (track 1, id 0x1100)
[10:29:44]      + bitrate: 384 kbps, samplerate: 48000 Hz
[10:29:44]    + mixdown: Dolby Pro Logic II
[10:29:44]    + encoder: AAC (faac)
[10:29:44]      + bitrate: 128 kbps, samplerate: 48000 Hz
[10:29:44] file is MPEG Transport Stream with 188 byte packets offset 0 bytes
[10:29:44] encx264: min-keyint: 60, keyint: 600
[10:29:44] encx264: Encoding at constant RF 20.000000
x264 [warning]: --psnr used with psy on: results will be invalid!
x264 [warning]: --tune psnr should be used if attempting to benchmark psnr!
[10:29:44] reader: first SCR 323335095 id 0x1011 DTS 323368443
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cach
e64
x264 [info]: profile Constrained Baseline, level 3.0
Encoding: task 1 of 1, Searching for start time, 77.29 % (ETA 00h00m30s)[10:31:2
8] next_packet: eof while re-establishing sync @ 6386434072
[10:31:28] hb_ts_stream_decode - eof
[10:31:28] reader: done. 1 scr changes
[10:31:28] reader: 7 drops because DTS out of range
[10:31:28] work: average encoding speed for job is 0.000000 fps
Encoding: task 1 of 1, Searching for start time, 77.30 % (ETA 00h00m30s)[10:31:2
8] sync: got 0 frames, 37822 expected
[10:31:28] render: lost time: 0 (0 frames)
[10:31:28] render: gained time: 0 (0 frames) (0 not accounted for)
[10:31:28] mux: track 0, 0 frames, 0 bytes, 0.00 kbps, fifo 8
[10:31:28] mux: track 1, 1 frames, 26 bytes, 12.47 kbps, fifo 8
[10:31:28] stream: 215038 good frames, 0 errors (0%)
[10:31:28] libhb: work result = 0

Encode done!

HandBrake has exited.
RandomEngy
Novice
Posts: 50
Joined: Thu Mar 29, 2007 2:56 am

Re: Tutorial / "How-to" for Handbrake .NET

Post by RandomEngy »

If you're having a problem with the CLI you're probably not going to get around it by using the API directly. The main advantages of the API are pause/resume, static previews, performance and getting to avoid lots of parsing and serialization from the command line. The CLI is generally better maintained in terms of the encoding engine and basic encoding settings.
Deleted User 13735

Re: Tutorial / "How-to" for Handbrake .NET

Post by Deleted User 13735 »

I think it's probably errors in the ts files from my Hauppauge HD-PVR though.
That's really where you need to start. Most PVR files inherit GOP indexing errors from the transport stream, that are best fixed by running them through the "Quickstream Fix" utility in VideoRedo first. Others have reported similar success with TSMuxer although I haven't tested it.
Post Reply