Skip to content

Commit 55be4c8

Browse files
author
RandomEngy
committed
Changed preview to use calculated bitrate for the full encode when using Target Size.
1 parent 26f3d8c commit 55be4c8

4 files changed

Lines changed: 30 additions & 13 deletions

File tree

HandBrakeInterop/HandBrakeInterop/HandBrakeInstance.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

HandBrakeInterop/HandBrakeInterop/HbLib.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,9 +1135,6 @@ public struct ptw32_handle_t
11351135

11361136
public partial class HbLib
11371137
{
1138-
[DllImport("hb.dll", EntryPoint = "hb_calc_bitrate", CallingConvention = CallingConvention.Cdecl)]
1139-
public static extern int hb_calc_bitrate(ref hb_job_s job, int size);
1140-
11411138
[DllImport("hb.dll", EntryPoint = "hb_register_logger", CallingConvention = CallingConvention.Cdecl)]
11421139
public static extern void hb_register_logger(LoggingCallback callback);
11431140

VidCoder/ViewModel/EncodingViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,7 @@ public VideoEncodeRateType VideoEncodeRateType
16701670
}
16711671

16721672
this.NotifyPropertyChanged("VideoBitrate");
1673+
this.NotifyPropertyChanged("TargetSize");
16731674
}
16741675

16751676
if (value == VideoEncodeRateType.TargetSize)
@@ -1694,6 +1695,7 @@ public VideoEncodeRateType VideoEncodeRateType
16941695
}
16951696

16961697
this.NotifyPropertyChanged("TargetSize");
1698+
this.NotifyPropertyChanged("VideoBitrate");
16971699
}
16981700

16991701
this.mainViewModel.RefreshDestination();

VidCoder/ViewModel/PreviewViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ private void OnPreviewScanCompleted(object sender, EventArgs eventArgs)
707707
this.logger.Log(" Title: " + this.job.Title);
708708
this.logger.Log(" Preview #: " + this.SelectedPreview);
709709

710-
this.previewInstance.StartEncode(this.job, true, this.SelectedPreview, this.PreviewSeconds);
710+
this.previewInstance.StartEncode(this.job, true, this.SelectedPreview, this.PreviewSeconds, this.job.Length.TotalSeconds);
711711
}
712712
}
713713
}

0 commit comments

Comments
 (0)