示例#1
0
        /// <summary>
        /// Resize, trim, and save a video file.
        /// </summary>
        /// <param name="filename">Full path to the video to resize.</param>
        /// <param name="outFile">Full path to where the output file should be saved.</param>
        /// <param name="resizeValue">The height, width, or precentage to resize to, as specified by the resizeOption parameter.</param>
        /// <param name="resizeOption">The type of resize which resizeValue is referring to.</param>
        /// <param name="outTypeOption">The output video file type.</param>
        /// <param name="videoQuality">The quality of the output video. A value of 100 means 8kbps for full HD (1980x1080)</param>
        /// <param name="trimRange">The range, in seconds, to trim the video clip to. Set to null to not trim the clip.</param>
        /// <param name="jobProgressCallback">Delegate function which is called at regular intervals for progress updates during the job.</param>
        /// <returns>Void.</returns>
        private static void ProcessVideoFile(string filename, string outFile, int resizeValue, comboOptions resizeOption,
                                             videoOutTypeOptions outTypeOption = videoOutTypeOptions.WMV,
                                             int videoQuality = 100,
                                             Tuple <double, double> trimRange = null,
                                             VideoProgressDelegateCallback jobProgressCallback = null)
        {
            Job       j            = new Job();
            MediaItem mediaItem    = new MediaItem(filename);
            var       originalSize = mediaItem.OriginalVideoSize;
            // Get new dimensions
            Tuple <int, int> newSize = GetNewSize(resizeOption, originalSize.Width, originalSize.Height, resizeValue);
            // Round to the nearest 4 pixels - this is required by encoder
            // Encoder says the value must be an even integer between 64 and 4096 and a multiple of 4
            int newWidth  = Convert.ToInt32(Math.Round(newSize.Item1 / 4.0) * 4);
            int newHeight = Convert.ToInt32(Math.Round(newSize.Item2 / 4.0) * 4);

            if (newWidth < 64 || newHeight < 64 || newWidth > 4096 || newHeight > 4096)
            {
                throw new Exception("New height and width must be between 64 and 4096 pixels");
            }

            double bitsPerSecondPerPixel = 8000.0 / 2000000.0;  // Assume 8kbps for full HD
            int    bitRate = Convert.ToInt32(bitsPerSecondPerPixel * videoQuality * newWidth * newHeight / 100);

            WindowsMediaOutputFormat outFormat = new WindowsMediaOutputFormat();

            outFormat.AudioProfile             = new Microsoft.Expression.Encoder.Profiles.WmaAudioProfile();
            outFormat.VideoProfile             = new Microsoft.Expression.Encoder.Profiles.AdvancedVC1VideoProfile();
            outFormat.VideoProfile.AspectRatio = mediaItem.OriginalAspectRatio;
            outFormat.VideoProfile.AutoFit     = true;
            outFormat.VideoProfile.Bitrate     = new Microsoft.Expression.Encoder.Profiles.VariableUnconstrainedBitrate(bitRate);
            outFormat.VideoProfile.Size        = new Size(newWidth, newHeight);

            mediaItem.VideoResizeMode = VideoResizeMode.Letterbox;
            mediaItem.OutputFormat    = outFormat;

            if (!(trimRange == null))
            {
                Source source = mediaItem.Sources[0];
                source.Clips[0].StartTime = TimeSpan.FromSeconds(trimRange.Item1);
                source.Clips[0].EndTime   = TimeSpan.FromSeconds(trimRange.Item2);
            }

            mediaItem.OutputFileName = Path.GetFileName(outFile);

            j.MediaItems.Add(mediaItem);
            j.CreateSubfolder = false;
            j.OutputDirectory = Path.GetDirectoryName(outFile);

            if (jobProgressCallback != null)
            {
                j.EncodeProgress += new EventHandler <EncodeProgressEventArgs>(jobProgressCallback);
            }
            j.Encode();
            j.Dispose();
        }
示例#2
0
        /// <summary>
        /// Gets the output filename for a video file.
        /// </summary>
        /// <param name="filename">Full path of the input file.</param>
        /// <param name="outType">Output file type.</param>
        /// <returns></returns>
        private static string GetOutputFilename(string filename, videoOutTypeOptions outType, string suffix)
        {
            // Gets an output filename
            string extension = Path.GetExtension(filename);

            switch (outType)
            {
            case videoOutTypeOptions.WMV:
                extension = ".wmv";
                break;
            }
            return(_CreateOutputFilename(filename, extension, suffix));
        }
示例#3
0
 public MediaProcessorOptions(comboOptions imageResizeType,
                              comboOptions videoResizeType,
                              outTypeOptions imageOutType,
                              videoOutTypeOptions videoOutType,
                              int imageResizeValue,
                              int videoResizeValue,
                              int jpegQuality,
                              int videoQuality,
                              float?defaultDropRatio)
 {
     this.imageResizeType  = imageResizeType;
     this.videoResizeType  = videoResizeType;
     this.imageOutType     = imageOutType;
     this.videoOutType     = videoOutType;
     this.imageResizeValue = imageResizeValue;
     this.videoResizeValue = videoResizeValue;
     this.jpegQuality      = jpegQuality;
     this.videoQuality     = videoQuality;
     this.defaultCropRatio = defaultDropRatio;
 }
示例#4
0
        private void btnProcess_Click(object sender, EventArgs e)
        {
            if (this.isProcessing)
            {
                this.cancelSource.Cancel();
                return;
            }
            if (this.fileList.Count == 0)
            {
                MessageBox.Show("No files selected.", "No files selected", MessageBoxButtons.OK);
                return;
            }
            int resizeValue = TryReadResizeText(this.txtResize, (comboOptions)this.cbxResizeType.SelectedIndex);

            if (resizeValue < 0)
            {
                MessageBox.Show("Invalid value entered for resizing images to.", "Invalid entry", MessageBoxButtons.OK);
                return;
            }
            int videoResizeValue = TryReadResizeText(this.txtVideoResize, (comboOptions)this.cbxVideoResizeType.SelectedIndex);

            if (videoResizeValue < 0)
            {
                MessageBox.Show("Invalid value entered for resizing videos to.", "Invalid entry", MessageBoxButtons.OK);
                return;
            }

            this.OnProcessingStart();



            comboOptions        comboOption        = (comboOptions)this.cbxResizeType.SelectedIndex;
            comboOptions        videoComboOption   = (comboOptions)this.cbxVideoResizeType.SelectedIndex;
            outTypeOptions      outTypeOption      = (outTypeOptions)this.cbxOutputType.SelectedIndex;
            videoOutTypeOptions videoOutTypeOption = (videoOutTypeOptions)this.cbxVideoOutputType.SelectedIndex;
            int jpegQuality         = (int)nudQuality.Value;
            int mpegQuality         = (int)nudVideoQuality.Value;
            CancellationToken token = this.cancelSource.Token;

            Task processingTask = Task.Factory.StartNew(() =>
            {
                List <string> failedList = new List <string>();
                for (int ii = 0; ii < this.fileList.Count; ii++)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                    try
                    {
                        if (!IsVideoFile(this.fileList[ii]))
                        {
                            ProcessImageFile(this.fileList[ii], resizeValue, comboOption, outTypeOption, jpegQuality);
                        }
                        else
                        {
                            Tuple <double, double> trimRange = null;
                            int idx = this.trimFiles.FindIndex(a => a == this.fileList[ii]);
                            if (idx >= 0)
                            {
                                trimRange = this.trimRanges[idx];
                            }
                            ProcessVideoFile(this.fileList[ii], videoResizeValue, videoComboOption, videoOutTypeOption, mpegQuality, trimRange);
                        }
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }
                        SetProgress(ii + 1);
                    }
                    catch (Exception exp)
                    {
                        Console.WriteLine(String.Format("{0} - {1}", exp.Message, exp.StackTrace));
                        failedList.Add(this.fileList[ii]);
                    }
                }
                if (token.IsCancellationRequested)
                {
                    this.OnProcessingCancelled();
                }
                else
                {
                    this.OnProcessingComplete(failedList.Count);
                }
            }, token);
        }
示例#5
0
        private void ProcessVideoFile(string filename, int resizeValue, comboOptions resizeOption,
                                      videoOutTypeOptions outTypeOption, int mpegQuality, Tuple <double, double> trimRange)
        {
            SetCurrentProgress(0, filename);

            // TODO - check if input file exists

            string outFile = GetOutputFilename(filename, outTypeOption);

            EnsureDir(outFile); // Will throw exception if can't create folder
            DialogResult result = WarnIfExists(outFile);

            switch (result)
            {
            case DialogResult.Yes:
                break;

            case DialogResult.No:
                return;

            case DialogResult.Cancel:
                this.cancelSource.Cancel();
                return;
            }

            Job       j            = new Job();
            MediaItem mediaItem    = new MediaItem(filename);
            var       originalSize = mediaItem.OriginalVideoSize;
            // Get new dimensions
            Tuple <int, int> newSize = GetNewSize(resizeOption, originalSize.Width, originalSize.Height, resizeValue);
            // Round to the nearest 4 pixels - this is required by encoder
            // Encoder says the value must be an even integer between 64 and 4096 and a multiple of 4
            int newWidth  = Convert.ToInt32(Math.Round(newSize.Item1 / 4.0) * 4);
            int newHeight = Convert.ToInt32(Math.Round(newSize.Item2 / 4.0) * 4);

            if (newWidth < 64 || newHeight < 64 || newWidth > 4096 || newHeight > 4096)
            {
                throw new Exception("New height and width must be between 64 and 4096 pixels");
                // TODO - display this in the status bar
            }

            double bitsPerSecondPerPixel = 8000.0 / 2000000.0;  // Assume 8kbps for full HD
            int    bitRate = Convert.ToInt32(bitsPerSecondPerPixel * mpegQuality * newWidth * newHeight / 100);

            WindowsMediaOutputFormat outFormat = new WindowsMediaOutputFormat();

            outFormat.AudioProfile = new Microsoft.Expression.Encoder.Profiles.WmaAudioProfile();
            // outFormat.VideoProfile = new Microsoft.Expression.Encoder.Profiles.MainVC1VideoProfile();
            outFormat.VideoProfile             = new Microsoft.Expression.Encoder.Profiles.AdvancedVC1VideoProfile();
            outFormat.VideoProfile.AspectRatio = mediaItem.OriginalAspectRatio;
            outFormat.VideoProfile.AutoFit     = true;
            outFormat.VideoProfile.Bitrate     = new Microsoft.Expression.Encoder.Profiles.VariableUnconstrainedBitrate(bitRate);
            outFormat.VideoProfile.Size        = new Size(newWidth, newHeight);

            //mediaItem.VideoResizeMode = VideoResizeMode.Letterbox;

            mediaItem.OutputFormat = outFormat;

            if (!(trimRange == null))
            {
                Source source = mediaItem.Sources[0];
                source.Clips[0].StartTime = TimeSpan.FromSeconds(trimRange.Item1);
                source.Clips[0].EndTime   = TimeSpan.FromSeconds(trimRange.Item2);
            }

            mediaItem.OutputFileName = Path.GetFileName(outFile);

            j.MediaItems.Add(mediaItem);
            j.CreateSubfolder = false;
            j.OutputDirectory = Path.GetDirectoryName(outFile);

            j.EncodeProgress += new EventHandler <EncodeProgressEventArgs>(OnJobEncodeProgress);
            j.Encode();
            j.Dispose();
        }