本文整理汇总了C#中HandBrake.Interop.Model.EncodeJob类的典型用法代码示例。如果您正苦于以下问题:C# EncodeJob类的具体用法?C# EncodeJob怎么用?C# EncodeJob使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncodeJob类属于HandBrake.Interop.Model命名空间,在下文中一共展示了EncodeJob类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateBitrate
/// <summary>
/// Calculates the video bitrate for the given job and target size.
/// </summary>
/// <param name="job">The encode job.</param>
/// <param name="sizeMB">The target size in MB.</param>
/// <param name="overallSelectedLengthSeconds">The currently selected encode length. Used in preview
/// for calculating bitrate when the target size would be wrong.</param>
/// <returns>The video bitrate in kbps.</returns>
public int CalculateBitrate(EncodeJob job, int sizeMB, double overallSelectedLengthSeconds = 0)
{
long availableBytes = ((long) sizeMB) * 1024 * 1024;
EncodingProfile profile = job.EncodingProfile;
Title title = this.GetTitle(job.Title);
double lengthSeconds = overallSelectedLengthSeconds > 0 ? overallSelectedLengthSeconds : HandBrakeUtils.GetJobLengthSeconds(job, title);
lengthSeconds += 1.5;
double outputFramerate;
if (profile.Framerate == 0)
{
outputFramerate = title.Framerate;
}
else
{
// Not sure what to do for VFR here hb_calc_bitrate never handled it...
// just use the peak for now.
outputFramerate = profile.Framerate;
}
long frames = (long)(lengthSeconds * outputFramerate);
availableBytes -= frames * HandBrakeUtils.ContainerOverheadPerFrame;
List<Tuple<AudioEncoding, int>> outputTrackList = this.GetOutputTracks(job, title);
availableBytes -= HandBrakeUtils.GetAudioSize(job, lengthSeconds, title, outputTrackList);
if (availableBytes < 0)
{
return 0;
}
// Video bitrate is in kilobits per second, or where 1 kbps is 1000 bits per second.
// So 1 kbps is 125 bytes per second.
return (int)(availableBytes / (125 * lengthSeconds));
}
示例2: CalculateFileSize
/// <summary>
/// Gives estimated file size (in MB) of the given job and video bitrate.
/// </summary>
/// <param name="job">The encode job.</param>
/// <param name="videoBitrate">The video bitrate to be used (kbps).</param>
/// <returns>The estimated file size (in MB) of the given job and video bitrate.</returns>
public double CalculateFileSize(EncodeJob job, int videoBitrate)
{
long totalBytes = 0;
EncodingProfile profile = job.EncodingProfile;
Title title = this.GetTitle(job.Title);
double lengthSeconds = HandBrakeUtils.GetJobLengthSeconds(job, title);
lengthSeconds += 1.5;
double outputFramerate;
if (profile.Framerate == 0)
{
outputFramerate = title.Framerate;
}
else
{
// Not sure what to do for VFR here hb_calc_bitrate never handled it...
// just use the peak for now.
outputFramerate = profile.Framerate;
}
long frames = (long)(lengthSeconds * outputFramerate);
totalBytes += (long)(lengthSeconds * videoBitrate * 125);
totalBytes += frames * HandBrakeUtils.ContainerOverheadPerFrame;
List<Tuple<AudioEncoding, int>> outputTrackList = this.GetOutputTracks(job, title);
totalBytes += HandBrakeUtils.GetAudioSize(job, lengthSeconds, title, outputTrackList);
return (double)totalBytes / 1024 / 1024;
}
示例3: GetEncodeJob
/*
* TODO: This conversion class needs to be finished off before libencode will work.
*/
/// <summary>
/// Get an EncodeJob model for a LibHB Encode.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
/// <returns>
/// An Interop.EncodeJob model.
/// </returns>
public static EncodeJob GetEncodeJob(QueueTask task)
{
// Sanity Checking
if (task == null || task.Task == null)
{
return null;
}
// The current Job Configuration
EncodeTask work = task.Task;
// Which will be converted to this EncodeJob Model.
EncodeJob job = new EncodeJob();
EncodingProfile profile = new EncodingProfile();
job.EncodingProfile = profile;
switch (work.Anamorphic)
{
case Model.Encoding.Anamorphic.Custom:
profile.Anamorphic = Interop.Model.Encoding.Anamorphic.Custom;
break;
case Model.Encoding.Anamorphic.Strict:
profile.Anamorphic = Interop.Model.Encoding.Anamorphic.Strict;
break;
case Model.Encoding.Anamorphic.Loose:
profile.Anamorphic = Interop.Model.Encoding.Anamorphic.Loose;
break;
case Model.Encoding.Anamorphic.None:
profile.Anamorphic = Interop.Model.Encoding.Anamorphic.None;
break;
}
profile.AudioEncodings = new List<AudioEncoding>();
foreach (AudioTrack track in work.AudioTracks)
{
AudioEncoding newTrack = new AudioEncoding
{
Bitrate = track.Bitrate,
Drc = track.DRC,
Gain = track.Gain,
//Encoder = track.Encoder,
// InputNumber = track.Track,
//Mixdown = track.MixDown,
//SampleRateRaw = track.SampleRate
};
profile.AudioEncodings.Add(newTrack);
}
profile.Cropping = new HandBrake.Interop.Model.Cropping
{
Top = work.Cropping.Top,
Bottom = work.Cropping.Bottom,
Left = work.Cropping.Left,
Right = work.Cropping.Right
};
profile.CustomCropping = true;
profile.CustomDecomb = work.CustomDecomb;
profile.CustomDeinterlace = work.CustomDeinterlace;
profile.CustomDenoise = work.CustomDenoise;
profile.CustomDetelecine = work.CustomDetelecine;
profile.Deblock = work.Deblock;
profile.Decomb = work.Decomb;
profile.Deinterlace = work.Deinterlace;
profile.Denoise = work.Denoise;
profile.Detelecine = work.Detelecine;
profile.DisplayWidth = work.DisplayWidth.HasValue
? int.Parse(Math.Round(work.DisplayWidth.Value, 0).ToString())
: 0;
profile.Framerate = work.Framerate.HasValue ? work.Framerate.Value : 0;
profile.Grayscale = work.Grayscale;
profile.Height = work.Height.HasValue ? work.Height.Value : 0;
profile.IPod5GSupport = work.IPod5GSupport;
profile.IncludeChapterMarkers = work.IncludeChapterMarkers;
profile.KeepDisplayAspect = work.KeepDisplayAspect;
profile.LargeFile = work.LargeFile;
profile.MaxHeight = work.MaxHeight.HasValue ? work.MaxHeight.Value : 0;
profile.MaxWidth = work.MaxWidth.HasValue ? work.MaxWidth.Value : 0;
profile.Modulus = work.Modulus.HasValue ? work.Modulus.Value : 16;
profile.Optimize = work.OptimizeMP4;
switch (work.OutputFormat)
{
case OutputFormat.Mp4:
case OutputFormat.M4V:
profile.OutputFormat = Interop.Model.Encoding.OutputFormat.Mp4;
break;
case OutputFormat.Mkv:
//.........这里部分代码省略.........
示例4: GetOutputTracks
/// <summary>
/// Gets a list of encodings and target track indices (1-based).
/// </summary>
/// <param name="job">The encode job</param>
/// <param name="title">The title the job is meant to encode.</param>
/// <returns>A list of encodings and target track indices (1-based).</returns>
private List<Tuple<AudioEncoding, int>> GetOutputTracks(EncodeJob job, Title title)
{
var list = new List<Tuple<AudioEncoding, int>>();
foreach (AudioEncoding encoding in job.EncodingProfile.AudioEncodings)
{
if (encoding.InputNumber == 0)
{
// Add this encoding for all chosen tracks
foreach (int chosenTrack in job.ChosenAudioTracks)
{
// In normal cases we'll never have a chosen audio track that doesn't exist but when batch encoding
// we just choose the first audio track without checking if it exists.
if (chosenTrack <= title.AudioTracks.Count)
{
list.Add(new Tuple<AudioEncoding, int>(encoding, chosenTrack));
}
}
}
else if (encoding.InputNumber <= job.ChosenAudioTracks.Count)
{
// Add this encoding for the specified track, if it exists
int trackNumber = job.ChosenAudioTracks[encoding.InputNumber - 1];
// In normal cases we'll never have a chosen audio track that doesn't exist but when batch encoding
// we just choose the first audio track without checking if it exists.
if (trackNumber <= title.AudioTracks.Count)
{
list.Add(new Tuple<AudioEncoding, int>(encoding, trackNumber));
}
}
}
return list;
}
示例5: ApplyJob
/// <summary>
/// Applies the encoding job to the native memory structure and returns a list of memory
/// locations allocated during this.
/// </summary>
/// <param name="nativeJob">The native structure to apply to job info to.</param>
/// <param name="job">The job info to apply.</param>
/// <returns>The list of memory locations allocated for the job.</returns>
private List<IntPtr> ApplyJob(ref hb_job_s nativeJob, EncodeJob job)
{
return this.ApplyJob(ref nativeJob, job, false, 0, 0, 0);
}
示例6: GetAudioSize
/// <summary>
/// Gets the size in bytes for the audio with the given parameters.
/// </summary>
/// <param name="job">The encode job.</param>
/// <param name="lengthSeconds">The length of the encode in seconds.</param>
/// <param name="title">The title to encode.</param>
/// <param name="outputTrackList">The list of tracks to encode.</param>
/// <returns>The size in bytes for the audio with the given parameters.</returns>
internal static long GetAudioSize(EncodeJob job, double lengthSeconds, Title title, List<Tuple<AudioEncoding, int>> outputTrackList)
{
long audioBytes = 0;
foreach (Tuple<AudioEncoding, int> outputTrack in outputTrackList)
{
AudioEncoding encoding = outputTrack.Item1;
AudioTrack track = title.AudioTracks[outputTrack.Item2 - 1];
int samplesPerFrame = HandBrakeUtils.GetAudioSamplesPerFrame(encoding.Encoder);
int audioBitrate;
HBAudioEncoder audioEncoder = Encoders.GetAudioEncoder(encoding.Encoder);
if (audioEncoder.IsPassthrough)
{
// Input bitrate is in bits/second.
audioBitrate = track.Bitrate / 8;
}
else if (encoding.EncodeRateType == AudioEncodeRateType.Quality)
{
// Can't predict size of quality targeted audio encoding.
audioBitrate = 0;
}
else
{
int outputBitrate;
if (encoding.Bitrate > 0)
{
outputBitrate = encoding.Bitrate;
}
else
{
outputBitrate = Encoders.GetDefaultBitrate(
audioEncoder,
encoding.SampleRateRaw == 0 ? track.SampleRate : encoding.SampleRateRaw,
Encoders.SanitizeMixdown(Encoders.GetMixdown(encoding.Mixdown), audioEncoder, track.ChannelLayout));
}
// Output bitrate is in kbps.
audioBitrate = outputBitrate * 1000 / 8;
}
audioBytes += (long)(lengthSeconds * audioBitrate);
// Audio overhead
audioBytes += encoding.SampleRateRaw * ContainerOverheadPerFrame / samplesPerFrame;
}
return audioBytes;
}
示例7: GetEncodeJob
/// <summary>
/// Get an EncodeJob model for a LibHB Encode.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
/// <returns>
/// An Interop.EncodeJob model.
/// </returns>
public static EncodeJob GetEncodeJob(EncodeTask task)
{
// The current Job Configuration
EncodeTask work = task;
// Which will be converted to this EncodeJob Model.
EncodeJob job = new EncodeJob();
EncodingProfile profile = new EncodingProfile();
job.EncodingProfile = profile;
// Audio Settings
profile.AudioEncodings = new List<AudioEncoding>();
job.ChosenAudioTracks = new List<int>();
foreach (AudioTrack track in work.AudioTracks)
{
AudioEncoding newTrack = new AudioEncoding
{
Bitrate = track.Bitrate,
Drc = track.DRC,
Gain = track.Gain,
Encoder = Converters.GetCliAudioEncoder(track.Encoder),
InputNumber = track.Track.HasValue ? track.Track.Value : 0,
Mixdown = Converters.GetCliMixDown(track.MixDown),
SampleRateRaw = GetSampleRateRaw(track.SampleRate),
};
profile.AudioEncodings.Add(newTrack);
if (track.Track != null)
{
job.ChosenAudioTracks.Add(track.Track.Value);
}
}
// Title Settings
job.OutputPath = work.Destination;
job.SourcePath = work.Source;
job.Title = work.Title;
// job.SourceType = work.Type;
switch (work.PointToPointMode)
{
case PointToPointMode.Chapters:
job.RangeType = VideoRangeType.Chapters;
break;
case PointToPointMode.Seconds:
job.RangeType = VideoRangeType.Seconds;
break;
case PointToPointMode.Frames:
job.RangeType = VideoRangeType.Frames;
break;
}
if (work.PointToPointMode == PointToPointMode.Seconds)
{
job.SecondsEnd = work.EndPoint;
job.SecondsStart = work.StartPoint;
}
if (work.PointToPointMode == PointToPointMode.Chapters)
{
job.ChapterStart = work.StartPoint;
job.ChapterEnd = work.EndPoint;
}
if (work.PointToPointMode == PointToPointMode.Frames)
{
job.FramesEnd = work.EndPoint;
job.FramesStart = work.StartPoint;
}
job.Angle = work.Angle;
job.EncodingProfile = profile;
// Output Settings
profile.IPod5GSupport = work.IPod5GSupport;
profile.Optimize = work.OptimizeMP4;
switch (work.OutputFormat)
{
case OutputFormat.Mp4:
case OutputFormat.M4V:
profile.OutputFormat = Container.Mp4;
break;
case OutputFormat.Mkv:
profile.OutputFormat = Container.Mkv;
break;
}
// Picture Settings
profile.Anamorphic = work.Anamorphic;
profile.Cropping = new Cropping
{
Top = work.Cropping.Top,
Bottom = work.Cropping.Bottom,
Left = work.Cropping.Left,
//.........这里部分代码省略.........
示例8: StartEncode
/// <summary>
/// Starts an encode with the given job.
/// </summary>
/// <param name="jobToStart">The job to start.</param>
public void StartEncode(EncodeJob jobToStart)
{
this.StartEncode(jobToStart, false, 0, 0, 0);
}
示例9: StartEncode
/// <summary>
/// Starts an encode with the given job.
/// </summary>
/// <param name="job">The job to start.</param>
/// <param name="preview">True if this is a preview encode.</param>
/// <param name="previewNumber">The preview number to start the encode at (0-based).</param>
/// <param name="previewSeconds">The number of seconds in the preview.</param>
/// <param name="overallSelectedLengthSeconds">The currently selected encode length. Used in preview
/// for calculating bitrate when the target size would be wrong.</param>
public void StartEncode(EncodeJob job, bool preview, int previewNumber, int previewSeconds, double overallSelectedLengthSeconds)
{
EncodingProfile profile = job.EncodingProfile;
if (job.ChosenAudioTracks == null)
{
throw new ArgumentException("job.ChosenAudioTracks cannot be null.");
}
this.currentJob = job;
IntPtr nativeJobPtr = HBFunctions.hb_job_init_by_index(this.hbHandle, this.GetTitleIndex(job.Title));
var nativeJob = InteropUtilities.ReadStructure<hb_job_s>(nativeJobPtr);
this.encodeAllocatedMemory = this.ApplyJob(ref nativeJob, job, preview, previewNumber, previewSeconds, overallSelectedLengthSeconds);
this.subtitleScan = false;
if (job.Subtitles != null && job.Subtitles.SourceSubtitles != null)
{
foreach (SourceSubtitle subtitle in job.Subtitles.SourceSubtitles)
{
if (subtitle.TrackNumber == 0)
{
this.subtitleScan = true;
break;
}
}
}
string x264Options = profile.X264Options ?? string.Empty;
IntPtr originalX264Options = Marshal.StringToHGlobalAnsi(x264Options);
this.encodeAllocatedMemory.Add(originalX264Options);
if (!string.IsNullOrEmpty(profile.X264Profile))
{
nativeJob.h264_profile = Marshal.StringToHGlobalAnsi(profile.X264Profile);
this.encodeAllocatedMemory.Add(nativeJob.h264_profile);
}
if (!string.IsNullOrEmpty(profile.X264Preset))
{
nativeJob.x264_preset = Marshal.StringToHGlobalAnsi(profile.X264Preset);
this.encodeAllocatedMemory.Add(nativeJob.x264_preset);
}
if (profile.X264Tunes != null && profile.X264Tunes.Count > 0)
{
nativeJob.x264_tune = Marshal.StringToHGlobalAnsi(string.Join(",", profile.X264Tunes));
this.encodeAllocatedMemory.Add(nativeJob.x264_tune);
}
if (!string.IsNullOrEmpty(job.EncodingProfile.H264Level))
{
nativeJob.h264_level = Marshal.StringToHGlobalAnsi(job.EncodingProfile.H264Level);
this.encodeAllocatedMemory.Add(nativeJob.h264_level);
}
if (this.subtitleScan)
{
// If we need to scan subtitles, enqueue a pre-processing job to do that.
nativeJob.pass = -1;
nativeJob.indepth_scan = 1;
nativeJob.advanced_opts = IntPtr.Zero;
HBFunctions.hb_add(this.hbHandle, ref nativeJob);
}
nativeJob.indepth_scan = 0;
if (job.EncodingProfile.TwoPass)
{
// First pass. Apply turbo options if needed.
nativeJob.pass = 1;
string firstPassAdvancedOptions = x264Options;
if (job.EncodingProfile.TurboFirstPass)
{
if (firstPassAdvancedOptions == string.Empty)
{
firstPassAdvancedOptions = TurboX264Opts;
}
else
{
firstPassAdvancedOptions += ":" + TurboX264Opts;
}
}
nativeJob.advanced_opts = Marshal.StringToHGlobalAnsi(firstPassAdvancedOptions);
this.encodeAllocatedMemory.Add(nativeJob.advanced_opts);
HBFunctions.hb_add(this.hbHandle, ref nativeJob);
//.........这里部分代码省略.........
示例10: ApplyJob
/// <summary>
/// Applies the encoding job to the native memory structure and returns a list of memory
/// locations allocated during this.
/// </summary>
/// <param name="nativeJob">The native structure to apply to job info to.</param>
/// <param name="job">The job info to apply.</param>
/// <param name="preview">True if this is a preview encode.</param>
/// <param name="previewNumber">The preview number (0-based) to encode.</param>
/// <param name="previewSeconds">The number of seconds in the preview.</param>
/// <param name="overallSelectedLengthSeconds">The currently selected encode length. Used in preview
/// for calculating bitrate when the target size would be wrong.</param>
/// <returns>The list of memory locations allocated for the job.</returns>
private List<IntPtr> ApplyJob(ref hb_job_s nativeJob, EncodeJob job, bool preview, int previewNumber, int previewSeconds, double overallSelectedLengthSeconds)
{
var allocatedMemory = new List<IntPtr>();
Title title = this.GetTitle(job.Title);
hb_title_s originalTitle = this.GetOriginalTitle(job.Title);
EncodingProfile profile = job.EncodingProfile;
if (preview)
{
nativeJob.start_at_preview = previewNumber + 1;
nativeJob.seek_points = this.previewCount;
// There are 90,000 PTS per second.
nativeJob.pts_to_stop = previewSeconds * 90000;
}
else
{
switch (job.RangeType)
{
case VideoRangeType.All:
break;
case VideoRangeType.Chapters:
if (job.ChapterStart > 0 && job.ChapterEnd > 0)
{
nativeJob.chapter_start = job.ChapterStart;
nativeJob.chapter_end = job.ChapterEnd;
}
else
{
nativeJob.chapter_start = 1;
nativeJob.chapter_end = title.Chapters.Count;
}
break;
case VideoRangeType.Seconds:
if (job.SecondsStart < 0 || job.SecondsEnd < 0 || job.SecondsStart >= job.SecondsEnd)
{
throw new ArgumentException("Seconds range " + job.SecondsStart + "-" + job.SecondsEnd + " is invalid.", "job");
}
// If they've selected the "full" title duration, leave off the arguments to make it clean
if (job.SecondsStart > 0 || job.SecondsEnd < title.Duration.TotalSeconds)
{
// For some reason "pts_to_stop" actually means the number of pts to stop AFTER the start point.
nativeJob.pts_to_start = (int)(job.SecondsStart * 90000);
nativeJob.pts_to_stop = (int)((job.SecondsEnd - job.SecondsStart) * 90000);
}
break;
case VideoRangeType.Frames:
if (job.FramesStart < 0 || job.FramesEnd < 0 || job.FramesStart >= job.FramesEnd)
{
throw new ArgumentException("Frames range " + job.FramesStart + "-" + job.FramesEnd + " is invalid.", "job");
}
// "frame_to_stop" actually means the number of frames total to encode AFTER the start point.
nativeJob.frame_to_start = job.FramesStart;
nativeJob.frame_to_stop = job.FramesEnd - job.FramesStart;
break;
}
}
// Chapter markers
nativeJob.chapter_markers = profile.IncludeChapterMarkers ? 1 : 0;
List<IntPtr> nativeChapters = nativeJob.list_chapter.ToIntPtrList();
if (!preview && profile.IncludeChapterMarkers)
{
int numChapters = title.Chapters.Count;
if (job.UseDefaultChapterNames)
{
for (int i = 0; i < numChapters; i++)
{
if (i < nativeChapters.Count)
{
HBFunctions.hb_chapter_set_title(nativeChapters[i], "Chapter " + (i + 1));
}
}
}
else
{
for (int i = 0; i < numChapters; i++)
{
if (i < nativeChapters.Count && i < job.CustomChapterNames.Count)
{
IntPtr chapterNamePtr;
//.........这里部分代码省略.........
示例11: GetSize
/// <summary>
/// Gets the final size for a given encode job.
/// </summary>
/// <param name="job">The encode job to use.</param>
/// <param name="width">The storage width.</param>
/// <param name="height">The storage height.</param>
/// <param name="parWidth">The pixel aspect X number.</param>
/// <param name="parHeight">The pixel aspect Y number.</param>
public void GetSize(EncodeJob job, out int width, out int height, out int parWidth, out int parHeight)
{
Title title = this.GetTitle(job.Title);
if (job.EncodingProfile.Anamorphic == Anamorphic.None)
{
Size storageDimensions = CalculateNonAnamorphicOutput(job.EncodingProfile, title);
width = storageDimensions.Width;
height = storageDimensions.Height;
parWidth = 1;
parHeight = 1;
return;
}
IntPtr nativeJobPtr = HBFunctions.hb_job_init_by_index(this.hbHandle, this.GetTitleIndex(title));
var nativeJob = InteropUtilities.ReadStructure<hb_job_s>(nativeJobPtr);
List<IntPtr> allocatedMemory = this.ApplyJob(ref nativeJob, job);
InteropUtilities.FreeMemory(allocatedMemory);
InteropUtilities.CloseJob(nativeJobPtr);
// During the ApplyJob call, it modified nativeJob to have the correct width, height and PAR.
// We use those for the size.
width = nativeJob.width;
height = nativeJob.height;
parWidth = nativeJob.anamorphic.par_width;
parHeight = nativeJob.anamorphic.par_height;
}
示例12: GetPreview
/// <summary>
/// Gets an image for the given job and preview
/// </summary>
/// <remarks>
/// Only incorporates sizing and aspect ratio into preview image.
/// </remarks>
/// <param name="job">The encode job to preview.</param>
/// <param name="previewNumber">The index of the preview to get (0-based).</param>
/// <returns>An image with the requested preview.</returns>
public BitmapImage GetPreview(EncodeJob job, int previewNumber)
{
IntPtr nativeJobPtr = HBFunctions.hb_job_init_by_index(this.hbHandle, this.GetTitleIndex(job.Title));
var nativeJob = InteropUtilities.ReadStructure<hb_job_s>(nativeJobPtr);
List<IntPtr> allocatedMemory = this.ApplyJob(ref nativeJob, job);
// There are some problems with getting previews with deinterlacing. Disabling for now.
nativeJob.deinterlace = 0;
int outputWidth = nativeJob.width;
int outputHeight = nativeJob.height;
int imageBufferSize = outputWidth * outputHeight * 4;
IntPtr nativeBuffer = Marshal.AllocHGlobal(imageBufferSize);
allocatedMemory.Add(nativeBuffer);
HBFunctions.hb_get_preview(this.hbHandle, ref nativeJob, previewNumber, nativeBuffer);
// We've used the job to get the preview. Clean up the job.
InteropUtilities.CloseJob(nativeJobPtr);
// Copy the filled image buffer to a managed array.
byte[] managedBuffer = new byte[imageBufferSize];
Marshal.Copy(nativeBuffer, managedBuffer, 0, imageBufferSize);
// We've copied the data out of unmanaged memory. Clean up that memory now.
InteropUtilities.FreeMemory(allocatedMemory);
var bitmap = new System.Drawing.Bitmap(outputWidth, outputHeight);
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, outputWidth, outputHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
IntPtr ptr = bitmapData.Scan0;
for (int i = 0; i < nativeJob.height; i++)
{
Marshal.Copy(managedBuffer, i * nativeJob.width * 4, ptr, nativeJob.width * 4);
ptr = IntPtr.Add(ptr, bitmapData.Stride);
}
bitmap.UnlockBits(bitmapData);
using (var memoryStream = new MemoryStream())
{
try
{
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
}
finally
{
bitmap.Dispose();
}
var wpfBitmap = new BitmapImage();
wpfBitmap.BeginInit();
wpfBitmap.CacheOption = BitmapCacheOption.OnLoad;
wpfBitmap.StreamSource = memoryStream;
wpfBitmap.EndInit();
wpfBitmap.Freeze();
return wpfBitmap;
}
}
示例13: StartEncode
/// <summary>
/// Starts an encode with the given job.
/// </summary>
/// <param name="jobToStart">
/// The job to start.
/// </param>
/// <param name="scanPreviewCount">
/// The scan Preview Count.
/// </param>
public void StartEncode(EncodeJob jobToStart, int scanPreviewCount)
{
this.StartEncode(jobToStart, false, 0, 0, 0, scanPreviewCount);
}
示例14: GetPreview
/// <summary>
/// Gets an image for the given job and preview
/// </summary>
/// <remarks>
/// Only incorporates sizing and aspect ratio into preview image.
/// </remarks>
/// <param name="job">The encode job to preview.</param>
/// <param name="previewNumber">The index of the preview to get (0-based).</param>
/// <returns>An image with the requested preview.</returns>
public BitmapImage GetPreview(EncodeJob job, int previewNumber)
{
hb_title_s title = this.GetOriginalTitle(job.Title);
hb_job_s nativeJob = InteropUtilities.ReadStructure<hb_job_s>(title.job);
List<IntPtr> allocatedMemory = this.ApplyJob(ref nativeJob, job);
// There are some problems with getting previews with deinterlacing. Disabling for now.
nativeJob.deinterlace = 0;
// Create a new job pointer from our modified job object
IntPtr newJob = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_job_s)));
Marshal.StructureToPtr(nativeJob, newJob, false);
allocatedMemory.Add(newJob);
int outputWidth = nativeJob.width;
int outputHeight = nativeJob.height;
int imageBufferSize = outputWidth * outputHeight * 4;
IntPtr nativeBuffer = Marshal.AllocHGlobal(imageBufferSize);
allocatedMemory.Add(nativeBuffer);
HBFunctions.hb_set_job(this.hbHandle, job.Title, ref nativeJob);
HBFunctions.hb_get_preview(this.hbHandle, ref title, previewNumber, nativeBuffer);
// Copy the filled image buffer to a managed array.
byte[] managedBuffer = new byte[imageBufferSize];
Marshal.Copy(nativeBuffer, managedBuffer, 0, imageBufferSize);
InteropUtilities.FreeMemory(allocatedMemory);
var bitmap = new System.Drawing.Bitmap(outputWidth, outputHeight);
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, outputWidth, outputHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
IntPtr ptr = bitmapData.Scan0;
for (int i = 0; i < nativeJob.height; i++)
{
Marshal.Copy(managedBuffer, i * nativeJob.width * 4, ptr, nativeJob.width * 4);
ptr = IntPtr.Add(ptr, bitmapData.Stride);
}
bitmap.UnlockBits(bitmapData);
using (var memoryStream = new MemoryStream())
{
try
{
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
}
finally
{
bitmap.Dispose();
}
var wpfBitmap = new BitmapImage();
wpfBitmap.BeginInit();
wpfBitmap.CacheOption = BitmapCacheOption.OnLoad;
wpfBitmap.StreamSource = memoryStream;
wpfBitmap.EndInit();
wpfBitmap.Freeze();
return wpfBitmap;
}
}
示例15: GetAudioSize
/// <summary>
/// Gets the size in bytes for the audio with the given parameters.
/// </summary>
/// <param name="job">The encode job.</param>
/// <param name="lengthSeconds">The length of the encode in seconds.</param>
/// <param name="title">The title to encode.</param>
/// <param name="outputTrackList">The list of tracks to encode.</param>
/// <returns>The size in bytes for the audio with the given parameters.</returns>
internal static long GetAudioSize(EncodeJob job, double lengthSeconds, Title title, List<Tuple<AudioEncoding, int>> outputTrackList)
{
long audioBytes = 0;
foreach (Tuple<AudioEncoding, int> outputTrack in outputTrackList)
{
AudioEncoding encoding = outputTrack.Item1;
AudioTrack track = title.AudioTracks[outputTrack.Item2 - 1];
int samplesPerFrame = HandBrakeUtils.GetAudioSamplesPerFrame(encoding.Encoder);
int audioBitrate;
if (Utilities.IsPassthrough(encoding.Encoder))
{
// Input bitrate is in bits/second.
audioBitrate = track.Bitrate / 8;
}
else
{
// Output bitrate is in kbps.
audioBitrate = encoding.Bitrate * 1000 / 8;
}
audioBytes += (long)(lengthSeconds * audioBitrate);
// Audio overhead
audioBytes += encoding.SampleRateRaw * ContainerOverheadPerFrame / samplesPerFrame;
}
return audioBytes;
}