Advertisement
Guest User

Untitled

a guest
Feb 27th, 2011
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using Tao.FFmpeg;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6.  
  7. using System.Runtime;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace TaoCheck
  11. {
  12.     class Program
  13.     {
  14.         static bool checkResult(int result, string atError)
  15.         {
  16.             if (result < 0)
  17.             {
  18.                 Console.WriteLine(atError);
  19.                 return false;
  20.             }
  21.             return true;
  22.         }
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             //Register all avaible codecs
  27.             FFmpeg.av_register_all();
  28.             FFmpeg.avcodec_register_all();
  29.  
  30.             //Open input file
  31.             IntPtr pFormatContext;
  32.             FFmpeg.AVFormatContext formatContext;
  33.             int res = FFmpeg.av_open_input_file(out pFormatContext, "1.avi", IntPtr.Zero, 0, IntPtr.Zero);
  34.             if (!checkResult(res, "Can't open file")) return;
  35.  
  36.             //Find streams info
  37.             res = FFmpeg.av_find_stream_info(pFormatContext);
  38.             if (!checkResult(res, "Can't find streams")) return;
  39.             formatContext = (FFmpeg.AVFormatContext)Marshal.PtrToStructure(pFormatContext, typeof(FFmpeg.AVFormatContext));
  40.  
  41.             //Find and open codec for video stream
  42.             FFmpeg.AVCodecContext? videoCodecCtx = null;
  43.             IntPtr pCodecContext = new IntPtr();
  44.             int videoStreamIndex = -1;
  45.             for (int i = 0; i < formatContext.nb_streams; i++)
  46.             {
  47.                 FFmpeg.AVStream stream = (FFmpeg.AVStream) Marshal.PtrToStructure(formatContext.streams[i], typeof(FFmpeg.AVStream));
  48.                 FFmpeg.AVCodecContext codec = (FFmpeg.AVCodecContext) Marshal.PtrToStructure(stream.codec, typeof(FFmpeg.AVCodecContext));
  49.                 if (codec.codec_type == FFmpeg.CodecType.CODEC_TYPE_VIDEO)
  50.                 {
  51.                     videoCodecCtx = codec;
  52.                     videoStreamIndex = i;
  53.                     pCodecContext = stream.codec;
  54.                 }
  55.             }
  56.             if (videoCodecCtx == null)
  57.             {
  58.                 Console.WriteLine("Video stream not found in file");
  59.                 return;
  60.             }
  61.             IntPtr pVideoCodec = FFmpeg.avcodec_find_decoder(videoCodecCtx.Value.codec_id);
  62.             if (pVideoCodec == IntPtr.Zero)
  63.             {
  64.                 Console.WriteLine("couldn't find codec");
  65.                 return;
  66.             }
  67.             res = FFmpeg.avcodec_open(pCodecContext, pVideoCodec );
  68.             if (!checkResult(res, "Can't open codec")) return;
  69.  
  70.             IntPtr pFrame = FFmpeg.avcodec_alloc_frame();
  71.             IntPtr pFrameRgb = FFmpeg.avcodec_alloc_frame();
  72.             FFmpeg.AVCodecContext ctx = videoCodecCtx.Value;
  73.  
  74.             IntPtr pPacket = Marshal.AllocHGlobal(56); //new FFmpeg.AVPacket()
  75.             //-----------------------------------------------------------------------------------------------------------------------------
  76.             //Read frames and decode it until get full picture and save it to file
  77.             int counter = 0;
  78.             while (FFmpeg.av_read_frame(pFormatContext, pPacket) >= 0)
  79.             {
  80.                 FFmpeg.AVPacket packet = (FFmpeg.AVPacket) Marshal.PtrToStructure(pPacket, typeof(FFmpeg.AVPacket));
  81.                 int finished = 0;
  82.                 FFmpeg.avcodec_decode_video(pCodecContext, pFrame, ref finished, packet.data, packet.size);
  83.                 if (packet.stream_index == videoStreamIndex)
  84.                 {
  85.                     if (finished!=0)
  86.                     {
  87.                         if (++counter <= 25)
  88.                         {
  89.                             Bitmap bmp = new Bitmap(ctx.width, ctx.height, PixelFormat.Format24bppRgb);
  90.                             BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
  91.                             FFmpeg.avpicture_fill(pFrameRgb, bd.Scan0, (int)FFmpeg.PixelFormat.PIX_FMT_RGB24, ctx.width, ctx.height);
  92.                             IntPtr swsContext = new IntPtr();
  93.                             swsContext = FFmpeg.sws_getCachedContext(swsContext, ctx.width, ctx.height, (int)ctx.pix_fmt, ctx.width, ctx.height, (int)FFmpeg.PixelFormat.PIX_FMT_RGB24, FFmpeg.SWS_FAST_BILINEAR, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  94.                             if (swsContext == IntPtr.Zero)
  95.                             {
  96.                                 Console.WriteLine("Not able to get libswscale context");
  97.                                 return;
  98.                             }
  99.                             FFmpeg.AVFrame src = (FFmpeg.AVFrame)Marshal.PtrToStructure(pFrame, typeof(FFmpeg.AVFrame));
  100.                             FFmpeg.AVFrame dst = (FFmpeg.AVFrame)Marshal.PtrToStructure(pFrameRgb, typeof(FFmpeg.AVFrame));
  101.                             //convert frame to selected format, this method used instead of deprecated img_convert
  102.                             FFmpeg.sws_scale(swsContext, src.data, src.linesize, 0, ctx.height, dst.data, dst.linesize);
  103.                             bmp.UnlockBits(bd);
  104.                             bmp.Save(String.Format("Frame{0}.png",counter));
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.             //Release used structures
  110.             Marshal.FreeHGlobal(pPacket);
  111.             FFmpeg.av_free(pFrame);
  112.             FFmpeg.av_free(pFrameRgb);
  113.             FFmpeg.avcodec_close(pCodecContext);
  114.             FFmpeg.av_close_input_file(pFormatContext);
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement