@@ -221,7 +221,7 @@ public BitmapImage GetPreview(EncodeJob job, int previewNumber)
221221 hb_title_s title = this . GetOriginalTitle ( job . Title ) ;
222222
223223 hb_job_s nativeJob = InteropUtilities . ReadStructure < hb_job_s > ( title . job ) ;
224- List < IntPtr > allocatedMemory = this . ApplyJob ( ref nativeJob , job , false , 0 , 0 ) ;
224+ List < IntPtr > allocatedMemory = this . ApplyJob ( ref nativeJob , job ) ;
225225
226226 // There are some problems with getting previews with deinterlacing. Disabling for now.
227227 nativeJob . deinterlace = 0 ;
@@ -285,15 +285,17 @@ public BitmapImage GetPreview(EncodeJob job, int previewNumber)
285285 /// </summary>
286286 /// <param name="job">The encode job.</param>
287287 /// <param name="sizeMB">The target size in MB.</param>
288+ /// <param name="overallSelectedLengthSeconds">The currently selected encode length. Used in preview
289+ /// for calculating bitrate when the target size would be wrong.</param>
288290 /// <returns>The video bitrate in kbps.</returns>
289- public int CalculateBitrate ( EncodeJob job , int sizeMB )
291+ public int CalculateBitrate ( EncodeJob job , int sizeMB , double overallSelectedLengthSeconds = 0 )
290292 {
291293 long availableBytes = sizeMB * 1024 * 1024 ;
292294
293295 EncodingProfile profile = job . EncodingProfile ;
294296 Title title = this . GetTitle ( job . Title ) ;
295297
296- double lengthSeconds = HandBrakeUtils . GetJobLengthSeconds ( job , title ) ;
298+ double lengthSeconds = overallSelectedLengthSeconds > 0 ? overallSelectedLengthSeconds : HandBrakeUtils . GetJobLengthSeconds ( job , title ) ;
297299 lengthSeconds += 1.5 ;
298300
299301 double outputFramerate ;
@@ -370,7 +372,7 @@ public double CalculateFileSize(EncodeJob job, int videoBitrate)
370372 /// <param name="jobToStart">The job to start.</param>
371373 public void StartEncode ( EncodeJob jobToStart )
372374 {
373- this . StartEncode ( jobToStart , false , 0 , 0 ) ;
375+ this . StartEncode ( jobToStart , false , 0 , 0 , 0 ) ;
374376 }
375377
376378 /// <summary>
@@ -380,11 +382,13 @@ public void StartEncode(EncodeJob jobToStart)
380382 /// <param name="preview">True if this is a preview encode.</param>
381383 /// <param name="previewNumber">The preview number to start the encode at (0-based).</param>
382384 /// <param name="previewSeconds">The number of seconds in the preview.</param>
383- public void StartEncode ( EncodeJob job , bool preview , int previewNumber , int previewSeconds )
385+ /// <param name="overallSelectedLengthSeconds">The currently selected encode length. Used in preview
386+ /// for calculating bitrate when the target size would be wrong.</param>
387+ public void StartEncode ( EncodeJob job , bool preview , int previewNumber , int previewSeconds , double overallSelectedLengthSeconds )
384388 {
385389 this . currentJob = job ;
386390 hb_job_s nativeJob = InteropUtilities . ReadStructure < hb_job_s > ( this . GetOriginalTitle ( job . Title ) . job ) ;
387- this . encodeAllocatedMemory = this . ApplyJob ( ref nativeJob , job , preview , previewNumber , previewSeconds ) ;
391+ this . encodeAllocatedMemory = this . ApplyJob ( ref nativeJob , job , preview , previewNumber , previewSeconds , overallSelectedLengthSeconds ) ;
388392
389393 if ( ! preview && job . EncodingProfile . IncludeChapterMarkers )
390394 {
@@ -549,7 +553,7 @@ public void GetSize(EncodeJob job, out int width, out int height, out int parWid
549553 }
550554
551555 var nativeJob = InteropUtilities . ReadStructure < hb_job_s > ( this . GetOriginalTitle ( job . Title ) . job ) ;
552- List < IntPtr > allocatedMemory = this . ApplyJob ( ref nativeJob , job , false , 0 , 0 ) ;
556+ List < IntPtr > allocatedMemory = this . ApplyJob ( ref nativeJob , job ) ;
553557
554558 int refWidth = 0 ;
555559 int refHeight = 0 ;
@@ -748,6 +752,18 @@ private void PollEncodeProgress()
748752 }
749753 }
750754
755+ /// <summary>
756+ /// Applies the encoding job to the native memory structure and returns a list of memory
757+ /// locations allocated during this.
758+ /// </summary>
759+ /// <param name="nativeJob">The native structure to apply to job info to.</param>
760+ /// <param name="job">The job info to apply.</param>
761+ /// <returns>The list of memory locations allocated for the job.</returns>
762+ private List < IntPtr > ApplyJob ( ref hb_job_s nativeJob , EncodeJob job )
763+ {
764+ return this . ApplyJob ( ref nativeJob , job , false , 0 , 0 , 0 ) ;
765+ }
766+
751767 /// <summary>
752768 /// Applies the encoding job to the native memory structure and returns a list of memory
753769 /// locations allocated during this.
@@ -757,8 +773,10 @@ private void PollEncodeProgress()
757773 /// <param name="preview">True if this is a preview encode.</param>
758774 /// <param name="previewNumber">The preview number (0-based) to encode.</param>
759775 /// <param name="previewSeconds">The number of seconds in the preview.</param>
776+ /// <param name="overallSelectedLengthSeconds">The currently selected encode length. Used in preview
777+ /// for calculating bitrate when the target size would be wrong.</param>
760778 /// <returns>The list of memory locations allocated for the job.</returns>
761- private List < IntPtr > ApplyJob ( ref hb_job_s nativeJob , EncodeJob job , bool preview = false , int previewNumber = 0 , int previewSeconds = 0 )
779+ private List < IntPtr > ApplyJob ( ref hb_job_s nativeJob , EncodeJob job , bool preview , int previewNumber , int previewSeconds , double overallSelectedLengthSeconds )
762780 {
763781 var allocatedMemory = new List < IntPtr > ( ) ;
764782 Title title = this . GetTitle ( job . Title ) ;
@@ -1192,7 +1210,7 @@ private List<IntPtr> ApplyJob(ref hb_job_s nativeJob, EncodeJob job, bool previe
11921210 break ;
11931211 case VideoEncodeRateType . TargetSize :
11941212 nativeJob . vquality = - 1 ;
1195- nativeJob . vbitrate = HbLib . hb_calc_bitrate ( ref nativeJob , profile . TargetSize ) ;
1213+ nativeJob . vbitrate = this . CalculateBitrate ( job , profile . TargetSize , overallSelectedLengthSeconds ) ;
11961214 break ;
11971215 default :
11981216 break ;
0 commit comments