Ejemplo n.º 1
0
        private void encode()
        {
            try {
                string sTempFileName = saveScriptToTempFile();
                try {
                    using (AviSynthScriptEnvironment env = new AviSynthScriptEnvironment()) {
                        using (AviSynthClip x = env.ParseScript(m_script)) { //.OpenScriptFile(sTempFileName))
                            if (0 == x.SamplesCount)
                            {
                                throw new ApplicationException("Can't find audio stream!");
                            }
                            audioStreamFound();
                            readAudioStreamInfo(x);
                            audioStreamInfo(x);
                            const int MAX_SAMPLES_PER_ONCE = 4096;
                            int       frameSample          = 0;
                            int       samplesRead          = 0;
                            int       frameBufferTotalSize = MAX_SAMPLES_PER_ONCE * x.ChannelsCount * x.BitsPerSample / 8;
                            int       bytesRead            = 0; //frameBufferTotalSize;
                            byte[]    frameBuffer          = new byte[frameBufferTotalSize];
                            bool      WExtHeader           = m_ChannelMask >= 0;
                            bool      Greater4GB           = m_nSizeInBytes >= (uint.MaxValue - 68);
                            if ((Greater4GB) && (m_HeaderType > 2))
                            {
                                m_HeaderType -= 2;
                            }
                            if (m_encoder != null)
                            {
                                createEncoderProcess(x);
                            }
                            try {
                                using (Stream target = getOutputStream())
                                {
                                    // let's write WAV Header
                                    if (m_sendHeader)
                                    {
                                        if (m_HeaderType == 1)                        // Useful for debug
                                        {
                                            if (WExtHeader)
                                            {
                                                raiseEvent(m_job, new EncoderCallbackEventArgs("Writing W64_EXT header to encoder's StdIn"));
                                            }
                                            else
                                            {
                                                raiseEvent(m_job, new EncoderCallbackEventArgs("Writing W64 header to encoder's StdIn"));
                                            }
                                        }
                                        else if (m_HeaderType == 2)
                                        {
                                            if (WExtHeader)
                                            {
                                                raiseEvent(m_job, new EncoderCallbackEventArgs("Writing RF64_EXT header to encoder's StdIn"));
                                            }
                                            else
                                            {
                                                raiseEvent(m_job, new EncoderCallbackEventArgs("Writing RF64 header to encoder's StdIn"));
                                            }
                                        }
                                        else
                                        {
                                            if (WExtHeader)
                                            {
                                                raiseEvent(m_job, new EncoderCallbackEventArgs("Writing RIFF_EXT header to encoder's StdIn"));
                                            }
                                            else
                                            {
                                                raiseEvent(m_job, new EncoderCallbackEventArgs("Writing RIFF header to encoder's StdIn"));
                                            }
                                        }
                                        writeHeader(target, x);
                                    }

                                    raiseEvent(m_job, new EncoderCallbackEventArgs("Writing PCM data to encoder's StdIn"));
                                    GCHandle h = GCHandle.Alloc(frameBuffer, GCHandleType.Pinned);
                                    try {
                                        while (frameSample < x.SamplesCount)
                                        {
                                            if (m_process != null)
                                            {
                                                if (m_process.HasExited)
                                                {
                                                    throw new ApplicationException("Abnormal encoder termination " + m_process.ExitCode.ToString());
                                                }
                                            }
                                            samplesRead = 0;
                                            bytesRead   = 0;

                                            int nHowMany = Math.Min((int)(x.SamplesCount - frameSample), MAX_SAMPLES_PER_ONCE);

                                            x.ReadAudio(h.AddrOfPinnedObject(), frameSample, nHowMany);
                                            bytesRead   = nHowMany * x.BytesPerSample * x.ChannelsCount;
                                            samplesRead = nHowMany;
                                            //setProgress((100*(double)frameSample / x.SamplesCount));
                                            m_job.Progress = (int)(100 * (double)frameSample / x.SamplesCount);
                                            target.Write(frameBuffer, 0, bytesRead);
                                            if (m_process != null)
                                            {
                                                target.Flush();
                                            }

                                            frameSample += samplesRead;

                                            Thread.Sleep(0);
                                        }
                                    }
                                    finally {
                                        h.Free();
                                    }

                                    //setProgress(100);
                                    m_job.Progress = 100;
                                }
                                if (m_process != null)
                                {
                                    raiseEvent(m_job, new EncoderCallbackEventArgs("Finalizing encoder"));
                                    m_process.WaitForExit();
                                    m_readFromStdErrThread.Join();
                                    m_readFromStdOutThread.Join();
                                    if (0 != m_process.ExitCode)
                                    {
                                        throw new ApplicationException("Abnormal encoder termination " + m_process.ExitCode.ToString());
                                    }
                                }
                            }
                            finally {
                                if (m_process != null)
                                {
                                    if (!m_process.HasExited)
                                    {
                                        m_process.Kill();
                                        m_process.WaitForExit();
                                        m_readFromStdErrThread.Join();
                                        m_readFromStdOutThread.Join();
                                    }
                                }
                                m_readFromStdErrThread = null;
                                m_readFromStdOutThread = null;
                            }
                        }
                    }
                }
                finally {
                    File.Delete(sTempFileName);
                }
            }
            catch (Exception e) {
                clearOutput();
                if (e is ThreadAbortException)
                {
                    raiseEvent(m_job, new EncoderCallbackEventArgs(EncoderCallbackEventArgs.EventType.Terminated));
                }
                else
                {
                    raiseEvent(m_job, new EncoderCallbackEventArgs(e));
                }
                return;
            }
            raiseEvent(m_job, new EncoderCallbackEventArgs(EncoderCallbackEventArgs.EventType.Done));
        }
Ejemplo n.º 2
0
 public AviSynthClip(string func, string arg, AviSynthColorspace forceColorspace, AviSynthScriptEnvironment env)
 {
     _vi         = new AVSDLLVideoInfo();
     _avs        = new IntPtr(0);
     _colorSpace = AviSynthColorspace.Unknown;
     _sampleType = AudioSampleType.Unknown;
     if (0 != dimzon_avs_init_2(ref _avs, func, arg, ref _vi, ref _colorSpace, ref _sampleType, forceColorspace.ToString()))
     {
         string err = getLastError();
         cleanup(false);
         throw new AviSynthException(err);
     }
 }