Esempio n. 1
0
        public static int GetSecondsTilDone(WMEncoder encoder, DateTime startTime)
        {
            int secsleft = -1;

            IWMEncStatistics  stats  = encoder.Statistics;
            IWMEncSourceGroup grp    = encoder.SourceGroupCollection.Item("SG_1");
            IWMEncAudioSource audsrc = (IWMEncAudioSource)grp.get_Source(WMENC_SOURCE_TYPE.WMENC_AUDIO, 0);

            double durationSecs     = (double)GetAudioSourceDuration(audsrc) / 1000F;
            double encodingTimeSecs = (double)(stats.EncodingTime * 10);

            double percentDone = encodingTimeSecs / durationSecs;

            TimeSpan ts = DateTime.UtcNow.Subtract(startTime);

            double estimatedTime = (ts.TotalSeconds / percentDone);

            secsleft = (int)(estimatedTime - ts.TotalSeconds);

            if (secsleft < 0)
            {
                secsleft = -1;
            }

            Logger.WriteLogMessage("Duration: " + durationSecs.ToString("G4") + " seconds, encoding source elapsed time: " + encodingTimeSecs.ToString("G4") + " seconds.");
            Logger.WriteLogMessage("Percent done: " + String.Format("{0:P}", percentDone) + ", encoding time left: " + secsleft + " seconds, estimated encoding time: " + estimatedTime.ToString("G4") + " seconds, elapsed time: " + ts.TotalSeconds.ToString("G4") + " seconds.");

            return(secsleft);
        }
Esempio n. 2
0
        private void Page_Load(object sender, System.EventArgs e)
        {
            WMEncoder encoder = new WMEncoder();

            WMEncProfile2 profile = new WMEncProfile2();

            profile.LoadFromFile(Server.MapPath("~/scmeda.prx"));

            IWMEncSourceGroupCollection srcGrpColl = encoder.SourceGroupCollection;
            IWMEncSourceGroup           srcGrp     = srcGrpColl.Add("SingleEncode");

            srcGrp.set_Profile(profile);

            IWMEncAudioSource audio = (WMEncoderLib.IWMEncAudioSource)srcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);

            audio.SetInput(Server.MapPath("~/cabeloEncolheu.wma"), "", "");
            audio.PreProcessPass = 0;

            IWMEncFile2 file = (IWMEncFile2)encoder.File;

            file.LocalFileName = Server.MapPath("~/cabeloEncolheu.mp3");

            encoder.PrepareToEncode(true);
            encoder.Start();
        }
Esempio n. 3
0
        private static int GetAudioSourceDuration(IWMEncAudioSource audsrc)
        {
            int clipduration = audsrc.MarkOut - audsrc.MarkIn;

            if (clipduration == 0)
                return audsrc.Duration;
            else
                return clipduration;
        }
Esempio n. 4
0
        private static int GetAudioSourceDuration(IWMEncAudioSource audsrc)
        {
            int clipduration = audsrc.MarkOut - audsrc.MarkIn;

            if (clipduration == 0)
            {
                return(audsrc.Duration);
            }
            else
            {
                return(clipduration);
            }
        }
Esempio n. 5
0
        /**
         * Set source.
         * @param type : source type.
         * @param name : source name.
         */
        public DVRBResult SetSource(ESourceType type, string name)
        {
            try
            {
                string       scheme = string.Empty;
                IWMEncSource source = null;
                switch (type)
                {
                case ESourceType.DeviceAudio:
                    scheme = "Device";
                    if (this.audioSource == null)
                    {
                        this.audioSource = (IWMEncAudioSource)this.sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
                    }

                    source = this.audioSource;
                    break;

                case ESourceType.DeviceVideo:
                    scheme = "Device";
                    if (this.videoSource == null)
                    {
                        this.videoSource = (IWMEncVideoSource)this.sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                    }

                    source = this.videoSource;
                    break;

                case ESourceType.DesktopVideo:
                    scheme = "ScreenCap";
                    if (this.videoSource == null)
                    {
                        this.videoSource = (IWMEncVideoSource)this.sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                    }

                    source = this.videoSource;
                    break;
                }

                log.Debug("Setting " + scheme + " -->" + name);
                source.SetInput(name, scheme, string.Empty);
            }
            catch (Exception e)
            {
                return(new DVRBResult(DVRBResult.ERROR, e.Message));
            }

            return(new DVRBResult());
        }
Esempio n. 6
0
        /**
         * Constructor.
         */
        public Encoder()
        {
            this.enableRecording = false;
            this.chopNumber      = 0;
            this.chopLength      = -1;

            this.choppingTimer                = new System.Timers.Timer();
            this.choppingTimer.Elapsed       += this.OnRecordTimeElapsed;
            this.archiveState                 = WMENC_ARCHIVE_STATE.WMENC_ARCHIVE_STOPPED;
            this.stateChangeWaitHandle        = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset, string.Empty);
            this.archiveStateChangeWaitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset, string.Empty);

            // Audio and video sources
            this.audioSource = null;
            this.videoSource = null;

            // Windows Media Encoder
            this.wmEncoder        = new WMEncoderClass();
            this.sourceGroup      = (IWMEncSourceGroup2)this.wmEncoder.SourceGroupCollection.Add("SG_1");
            this.wmEncoderProfile = new WMEncProfile2();

            // For now, we only listen these events
            this.wmEncoder.OnStateChange        += this.OnStateChange;
            this.wmEncoder.OnError              += this.OnError;
            this.wmEncoder.OnArchiveStateChange += this.OnArchiveStateChange;

            // Set recording props.
            this.wmEncoder.EnableAutoArchive = false;
            this.wmEncoder.AutoIndex         = false;

            // Set default profile values
            this.wmEncoderProfile.ValidateMode = true;
            this.wmEncoderProfile.ProfileName  = "Windows Media Encoder Profile";
            this.wmEncoderProfile.ContentType  = 17;                                        // Audio + Video
            this.wmEncoderAudience             = this.wmEncoderProfile.AddAudience(100000); // Initial bitrate = 100 kbps, will change automatically
            // when user sets video and audio params.
        }
Esempio n. 7
0
        /**
         * Set source.
         * @param type : source type.
         * @param name : source name.
         */
        public DVRBResult SetSource(ESourceType type, string name)
        {
            try
            {
                string scheme = string.Empty;
                IWMEncSource source = null;
                switch (type)
                {
                    case ESourceType.DeviceAudio:
                        scheme = "Device";
                        if (this.audioSource == null)
                        {
                            this.audioSource = (IWMEncAudioSource)this.sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
                        }

                        source = this.audioSource;
                        break;
                    case ESourceType.DeviceVideo:
                        scheme = "Device";
                        if (this.videoSource == null)
                        {
                            this.videoSource = (IWMEncVideoSource)this.sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                        }

                        source = this.videoSource;
                        break;
                    case ESourceType.DesktopVideo:
                        scheme = "ScreenCap";
                        if (this.videoSource == null)
                        {
                            this.videoSource = (IWMEncVideoSource)this.sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                        }

                        source = this.videoSource;
                        break;
                }

                log.Debug("Setting " + scheme + " -->" + name);
                source.SetInput(name, scheme, string.Empty);
            }
            catch (Exception e)
            {
                return new DVRBResult(DVRBResult.ERROR, e.Message);
            }

            return new DVRBResult();
        }
Esempio n. 8
0
        /**
         * Constructor.
         */
        public Encoder()
        {
            this.enableRecording = false;
            this.chopNumber = 0;
            this.chopLength = -1;

            this.choppingTimer = new System.Timers.Timer();
            this.choppingTimer.Elapsed += this.OnRecordTimeElapsed;
            this.archiveState = WMENC_ARCHIVE_STATE.WMENC_ARCHIVE_STOPPED;
            this.stateChangeWaitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset, string.Empty);
            this.archiveStateChangeWaitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset, string.Empty);

            // Audio and video sources
            this.audioSource = null;
            this.videoSource = null;

            // Windows Media Encoder
            this.wmEncoder = new WMEncoderClass();
            this.sourceGroup = (IWMEncSourceGroup2)this.wmEncoder.SourceGroupCollection.Add("SG_1");
            this.wmEncoderProfile = new WMEncProfile2();

            // For now, we only listen these events
            this.wmEncoder.OnStateChange += this.OnStateChange;
            this.wmEncoder.OnError += this.OnError;
            this.wmEncoder.OnArchiveStateChange += this.OnArchiveStateChange;

            // Set recording props.
            this.wmEncoder.EnableAutoArchive = false;
            this.wmEncoder.AutoIndex = false;

            // Set default profile values
            this.wmEncoderProfile.ValidateMode = true;
            this.wmEncoderProfile.ProfileName = "Windows Media Encoder Profile";
            this.wmEncoderProfile.ContentType = 17; // Audio + Video
            this.wmEncoderAudience = this.wmEncoderProfile.AddAudience(100000); // Initial bitrate = 100 kbps, will change automatically
            // when user sets video and audio params.
        }
Esempio n. 9
0
        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string                  glbstrErrLocation;
            WMEncoder               Encoder    = null;
            WMEncProfile2           Profile    = null;
            IWMDRMContentAuthor     DRMAuthor  = null;
            IWMDRMProfileCollection DRMProColl = null;
            IWMDRMProfile           DRMPro     = null;
            IWMEncSourceGroup       SrcGrp     = null;
            IWMEncAudioSource       SrcAud     = null;
            IWMEncVideoSource2      SrcVid     = null;

            _TwoPassEncoding = false;


            bool glbboolEncodingContinue = true;;

            DateTime time = DateTime.Now;

            try
            {
                Encoder = new WMEncoder();
                Encoder.OnStateChange += new _IWMEncoderEvents_OnStateChangeEventHandler(this.Encoder_OnStateChange);
                _SrcGrpColl            = Encoder.SourceGroupCollection;

                try
                {
                    DRMAuthor  = Encoder.EncoderDRMContentAuthor;
                    DRMProColl = DRMAuthor.DRMProfileCollection;
                    DRMPro     = null;

                    object vKeyID = null;
                    if (_encodeInfo.DRMProfile != "None")
                    {
                        int intDRMProCount = 0;
                        for (intDRMProCount = 0; intDRMProCount <= DRMProColl.Count - 1; intDRMProCount++)
                        {
                            if (DRMProColl.Item(intDRMProCount).Name == _encodeInfo.DRMProfile)
                            {
                                DRMPro = DRMProColl.Item(intDRMProCount);
                                break;
                            }
                        }
                        DRMAuthor.SetSessionDRMProfile(DRMPro.ID, ref vKeyID);
                    }
                    else
                    {
                        DRMAuthor.SetSessionDRMProfile(System.DBNull.Value.ToString(), ref vKeyID);
                        DRMAuthor  = null;
                        DRMProColl = null;
                        DRMPro     = null;
                        vKeyID     = null;
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Specify DRM Profile - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                Profile = new WMEncProfile2();
                Int32 intProContentType  = 0;
                int   intProVBRModeAudio = 0;
                int   intProVBRModeVideo = 0;
                try
                {
                    Profile.LoadFromFile(_encodeInfo.Profile);
                    intProContentType  = Profile.ContentType;
                    intProVBRModeAudio = (int)Profile.get_VBRMode(WMENC_SOURCE_TYPE.WMENC_AUDIO, 0);
                    intProVBRModeVideo = (int)Profile.get_VBRMode(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0);
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Load Profile - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }

                try
                {
                    SrcGrp = _SrcGrpColl.Add("BatchEncode");
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Source Group - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                string strDestExtension = System.IO.Path.GetExtension(_encodeInfo.Destination);

                try
                {
                    if (intProContentType == 1)
                    {
                        SrcAud = (WMEncoderLib.IWMEncAudioSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
                        SrcAud.SetInput(_encodeInfo.Source, "", "");
                    }
                    else if (intProContentType == 16)
                    {
                        SrcVid = (WMEncoderLib.IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                        SrcVid.SetInput(_encodeInfo.Source, "", "");
                    }
                    else if (intProContentType == 17)
                    {
                        SrcAud = (WMEncoderLib.IWMEncAudioSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
                        SrcAud.SetInput(_encodeInfo.Source, "", "");
                        SrcVid = (WMEncoderLib.IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                        SrcVid.SetInput(_encodeInfo.Source, "", "");
                    }
                    else
                    {
                        _backgroundWorker.ReportProgress(0, "BatchEncode does not support this type of profile");
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Video / Audio Source - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                try
                {
                    SrcGrp.set_Profile(Profile);
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Encoding Profile - " + ex.Message.ToString();

                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                IWMEncDisplayInfo Descr = Encoder.DisplayInfo;
                try
                {
                    if (_encodeInfo.Title != "")
                    {
                        Descr.Title = _encodeInfo.Title;
                    }
                    if (_encodeInfo.Description != "")
                    {
                        Descr.Description = _encodeInfo.Description;
                    }
                    if (_encodeInfo.Author != "")
                    {
                        Descr.Author = _encodeInfo.Author;
                    }
                    if (_encodeInfo.Copyright != "")
                    {
                        Descr.Copyright = _encodeInfo.Copyright;
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Display Information - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                try
                {
                    if (intProContentType == 16 || intProContentType == 17)
                    {
                        if (_encodeInfo.Crop == true)
                        {
                            SrcVid.CroppingBottomMargin = (int)_encodeInfo.CropBottom;
                            SrcVid.CroppingTopMargin    = (int)_encodeInfo.CropTop;
                            SrcVid.CroppingLeftMargin   = (int)_encodeInfo.CropLeft;
                            SrcVid.CroppingRightMargin  = (int)_encodeInfo.CropRight;
                        }
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Cropping - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                try
                {
                    if (intProContentType == 16 || intProContentType == 17)
                    {
                        SrcVid.Optimization = _encodeInfo.Preproc;
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Video Optimization - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }

                try
                {
                    if (intProContentType == 1)
                    {
                        if (_encodeInfo.TwoPass == false)
                        {
                            SrcAud.PreProcessPass = 0;
                            _TwoPassEncoding      = false;
                        }
                        else
                        {
                            SrcAud.PreProcessPass = 1;
                            _TwoPassEncoding      = true;
                            glbPassNumber         = 1;
                        }
                    }
                    else if (intProContentType == 16)
                    {
                        if (_encodeInfo.TwoPass == false)
                        {
                            SrcVid.PreProcessPass = 0;
                            _TwoPassEncoding      = false;
                        }
                        else
                        {
                            SrcVid.PreProcessPass = 1;
                            _TwoPassEncoding      = true;
                            glbPassNumber         = 1;
                        }
                    }
                    else if (intProContentType == 17)
                    {
                        if (_encodeInfo.TwoPass == false)
                        {
                            SrcAud.PreProcessPass = 0;
                            SrcVid.PreProcessPass = 0;
                            _TwoPassEncoding      = false;
                        }
                        else
                        {
                            switch (intProVBRModeAudio)
                            {
                            case 1:
                                SrcAud.PreProcessPass = 1;
                                break;

                            case 2:
                                SrcAud.PreProcessPass = 1;
                                break;

                            case 3:
                                SrcAud.PreProcessPass = 0;
                                break;

                            case 4:
                                SrcAud.PreProcessPass = 1;
                                break;
                            }
                            switch (intProVBRModeVideo)
                            {
                            case 1:
                                SrcVid.PreProcessPass = 1;
                                break;

                            case 2:
                                SrcVid.PreProcessPass = 1;
                                break;

                            case 3:
                                SrcVid.PreProcessPass = 0;
                                break;

                            case 4:
                                SrcVid.PreProcessPass = 1;
                                break;
                            }
                            _TwoPassEncoding = true;
                            glbPassNumber    = 1;
                        }
                    }
                    else
                    {
                        _backgroundWorker.ReportProgress(0, "BatchEncode does not support this type of profile");
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Passes - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                IWMEncFile2 File = (IWMEncFile2)Encoder.File;
                try
                {
                    File.LocalFileName = _encodeInfo.Destination;
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Output File - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                int intDurationAudio = 0;
                int intDurationVideo = 0;
                int intDurationFinal;
                try
                {
                    _backgroundWorker.ReportProgress(0, "Preparing to encode");
                    Encoder.PrepareToEncode(true);
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Encoder Prepare - " + ex.Message.ToString();


                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                try
                {
                    if (SrcAud != null)
                    {
                        intDurationAudio = System.Convert.ToInt32(SrcAud.Duration / 1000);
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    if (SrcVid != null)
                    {
                        intDurationVideo = System.Convert.ToInt32(SrcVid.Duration / 1000);
                    }
                }
                catch (Exception)
                {
                }
                if (intDurationAudio == 0)
                {
                    intDurationFinal = intDurationVideo;
                }
                else if (intDurationVideo == 0)
                {
                    intDurationFinal = intDurationAudio;
                }
                else
                {
                    if (intDurationVideo >= intDurationAudio)
                    {
                        intDurationFinal = intDurationVideo;
                    }
                    else
                    {
                        intDurationFinal = intDurationAudio;
                    }
                }
                glbintSourceDuration = intDurationFinal;
                try
                {
                    if (glbboolEncodingContinue == true)
                    {
                        Encoder.Start();
                        do
                        {
                            if (_backgroundWorker.CancellationPending)
                            {
                                Encoder.Stop();
                                e.Cancel = true;
                                _ev.Set();
                            }

                            sReportPercentComplete(Encoder);
                        }while (!_ev.WaitOne(1000));
                    }
                }
                catch (Exception ex)
                {
                    glbstrErrLocation = "Encoder Start - " + ex.Message.ToString();
                    throw new ApplicationException(glbstrErrLocation, ex);
                }
                if (e.Cancel)
                {
                    return;
                }
                else
                {
                    _backgroundWorker.ReportProgress(0, "Encoding Complete");
                    return;
                }
            }
            finally
            {
                if (_SrcGrpColl != null)
                {
                    try
                    {
                        Encoder.Stop();
                        _SrcGrpColl.Remove(0);
                    }
                    catch
                    {
                    }
                    Marshal.ReleaseComObject(_SrcGrpColl);
                    _SrcGrpColl = null;
                }
                if (Profile != null)
                {
                    Marshal.ReleaseComObject(Profile);
                    Profile = null;
                }
                if (Encoder != null)
                {
                    Encoder.OnStateChange -= new _IWMEncoderEvents_OnStateChangeEventHandler(this.Encoder_OnStateChange);
                    Marshal.ReleaseComObject(Encoder);
                    Encoder = null;
                }
                e.Result = DateTime.Now - time;
            }
        }