Inheritance: AVFrameAbs
Ejemplo n.º 1
0
    protected void Click_Handler(object sender, EventArgs e)
    {
        Gtk.FileChooserDialog dialog = new FileChooserDialog("Choose a file",
                                                             this,
                                                             FileChooserAction.Open,
                                                             new object[] {
            Gtk.Stock.Open,
            Gtk.ResponseType.Ok
        });
        dialog.Run();

        string file = dialog.Filename;

        dialog.Destroy();
        //writer.Seek(44, SeekOrigin.Begin);
        workingThread = new Thread(new ThreadStart(() =>
        {
            var stream     = FFMpegBase.Instance.GetAVStream(file);
            IAVFrame frame = null;
            while (((frame = stream.GetNext()) != null) && (!closing))
            {
                if (frame.FrameType == AVFrameType.Video)
                {
                    SharpFFmpeg.VideoFrame video = (SharpFFmpeg.VideoFrame)frame;
                    if (frame.Decode())
                    {
                        var data = video.ImgData;
                        DrawImage(data);
                    }
                }
                else if (frame.FrameType == AVFrameType.Audio)
                {
                    SharpFFmpeg.AudioFrame audio = (SharpFFmpeg.AudioFrame)frame;
                    if (audio.Decode())
                    {
                        var data = audio.WaveDate;
                        audioPlayer.PutSample(data);
                    }
                }
                frame.Close();
            }
            audioPlayer.Stop();
            stream.Close();
        }));
        workingThread.Start();
    }
Ejemplo n.º 2
0
        //private void WriteWaveHeader(BinaryWriter writer, WaveDataType format)
        //{
        //    WAVE_Header wav_Header = new WAVE_Header();
        //    wav_Header.RIFF_ID[0] = 'R';
        //    wav_Header.RIFF_ID[1] = 'I';
        //    wav_Header.RIFF_ID[2] = 'F';
        //    wav_Header.RIFF_ID[3] = 'F';
        //    wav_Header.File_Size = waveDataSize + 36;
        //    wav_Header.RIFF_Type[0] = 'W';
        //    wav_Header.RIFF_Type[1] = 'A';
        //    wav_Header.RIFF_Type[2] = 'V';
        //    wav_Header.RIFF_Type[3] = 'E';

        //    wav_Header.FMT_ID[0] = 'f';
        //    wav_Header.FMT_ID[1] = 'm';
        //    wav_Header.FMT_ID[2] = 't';
        //    wav_Header.FMT_ID[3] = ' ';
        //    wav_Header.FMT_Size = 16;
        //    wav_Header.FMT_Tag = 0x0001;
        //    wav_Header.FMT_Channel = (ushort)format.channel;
        //    wav_Header.FMT_SamplesPerSec = format.sample_rate;

        //    var nBlockAlign = (short)(format.channel * (format.bit_per_sample / 8));
        //    var nAvgBytesPerSec = format.sample_rate * nBlockAlign;

        //    wav_Header.AvgBytesPerSec = nAvgBytesPerSec;
        //    wav_Header.BlockAlign = (ushort)nBlockAlign;
        //    wav_Header.BitsPerSample = (ushort)format.bit_per_sample;

        //    wav_Header.DATA_ID[0] = 'd';
        //    wav_Header.DATA_ID[1] = 'a';
        //    wav_Header.DATA_ID[2] = 't';
        //    wav_Header.DATA_ID[3] = 'a';
        //    wav_Header.DATA_Size = waveDataSize;


        //    int waveHdrSize = Marshal.SizeOf(wav_Header);
        //    var ptr = Marshal.AllocHGlobal(waveHdrSize);
        //    byte[] data = new byte[waveHdrSize];
        //    Marshal.StructureToPtr(wav_Header, ptr, false);
        //    Marshal.Copy(ptr, data, 0, waveHdrSize);
        //    writer.Seek(0, SeekOrigin.Begin);
        //    writer.Write(data);
        //    writer.Flush();
        //    Marshal.FreeHGlobal(ptr);
        //}


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.ShowDialog();
            string file = dialog.FileName;
            //writer.Seek(44, SeekOrigin.Begin);
            WaveDataType first = null;

            workingThread = new Thread(new ThreadStart(() =>
            {
                var stream     = FFMpegBase.Instance.GetAVStream(file);
                IAVFrame frame = null;
                while (((frame = stream.GetNext()) != null) && (!closing))
                {
                    if (frame.FrameType == AVFrameType.Video)
                    {
                        SharpFFmpeg.VideoFrame video = (SharpFFmpeg.VideoFrame)frame;
                        if (frame.Decode())
                        {
                            var data = video.ImgData;
                            DrawImage(data);
                        }
                    }
                    else if (frame.FrameType == AVFrameType.Audio)
                    {
                        SharpFFmpeg.AudioFrame audio = (SharpFFmpeg.AudioFrame)frame;
                        if (audio.Decode())
                        {
                            var data = audio.WaveDate;
                            audioPlayer.PutSample(data);
                        }
                    }
                    frame.Close();
                }
                stream.Close();
                audioPlayer.Stop();
            }));
            workingThread.Start();
        }
Ejemplo n.º 3
0
        private void ConvertToBitmapAndDraw(VideoFrame This)
		{
            var type = This.ImgData;
            Stream str = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(str);
            // LITTLE ENDIAN!!
            writer.Write(new byte[] { 0x42, 0x4D });
            writer.Write((int)(type.managedData.Length + 0x36));
            writer.Write((int)0);
            writer.Write((int)0x36);
            writer.Write((int)40);
            writer.Write((int)type.width);
            writer.Write((int)type.height);
            writer.Write((short)1);
            writer.Write((short)24);
            writer.Write((int)0);
            writer.Write((int)type.managedData.Length);
            writer.Write((int)3780);
            writer.Write((int)3780);
            writer.Write((int)0);
            writer.Write((int)0);
            for (int y = type.height - 1; y >= 0; y--)
                writer.Write(type.managedData, y * type.linesize, type.width * 3);
            writer.Flush();
            writer.Seek(0, SeekOrigin.Begin);
            Bitmap bitmap = new Bitmap(str);
            videoGraphics.DrawImage(bitmap, 0, 0, videoWindowSize.Width, videoWindowSize.Height);
            writer.Close();
        }
Ejemplo n.º 4
0
        public IAVFrame GetNext()
        {
            IAVFrame frame = null;
            IntPtr pPacket = Marshal.AllocHGlobal(Marshal.SizeOf(new AV.AVPacket()));
            if (AV.av_read_frame(rawFormatCtx, pPacket) != 0)
            {
                Marshal.FreeHGlobal(pPacket);
                pPacket = IntPtr.Zero;
                return null;
            }


            AV.AVPacket packet = new NativeGetter<AV.AVPacket>(pPacket).Get();
            if (! decoderTable.ContainsKey(packet.stream_index) ||
                ! mediaTypeTable.ContainsKey(packet.stream_index))
            {
                Marshal.FreeHGlobal(pPacket);
                pPacket = IntPtr.Zero;
                return null;
            }

            var codec = decoderTable[packet.stream_index];
            var type = mediaTypeTable[packet.stream_index];
            switch(type)
            {
                case AV.AVMediaType.AVMEDIA_TYPE_AUDIO:
                    frame = new AudioFrame(pPacket, codec);
                    return frame;
                case AV.AVMediaType.AVMEDIA_TYPE_VIDEO:
                    frame = new VideoFrame(pPacket, codec);
                    return frame;
                default:
                    throw new Exception("Not support media type " + type.ToString());
            }
            return null;

        }