Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using Tao.FFmpeg;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Runtime;
- using System.Runtime.InteropServices;
- namespace TaoCheck
- {
- class Program
- {
- static bool checkResult(int result, string atError)
- {
- if (result < 0)
- {
- Console.WriteLine(atError);
- return false;
- }
- return true;
- }
- static void Main(string[] args)
- {
- //Register all avaible codecs
- FFmpeg.av_register_all();
- FFmpeg.avcodec_register_all();
- //Open input file
- IntPtr pFormatContext;
- FFmpeg.AVFormatContext formatContext;
- int res = FFmpeg.av_open_input_file(out pFormatContext, "1.avi", IntPtr.Zero, 0, IntPtr.Zero);
- if (!checkResult(res, "Can't open file")) return;
- //Find streams info
- res = FFmpeg.av_find_stream_info(pFormatContext);
- if (!checkResult(res, "Can't find streams")) return;
- formatContext = (FFmpeg.AVFormatContext)Marshal.PtrToStructure(pFormatContext, typeof(FFmpeg.AVFormatContext));
- //Find and open codec for video stream
- FFmpeg.AVCodecContext? videoCodecCtx = null;
- IntPtr pCodecContext = new IntPtr();
- int videoStreamIndex = -1;
- for (int i = 0; i < formatContext.nb_streams; i++)
- {
- FFmpeg.AVStream stream = (FFmpeg.AVStream) Marshal.PtrToStructure(formatContext.streams[i], typeof(FFmpeg.AVStream));
- FFmpeg.AVCodecContext codec = (FFmpeg.AVCodecContext) Marshal.PtrToStructure(stream.codec, typeof(FFmpeg.AVCodecContext));
- if (codec.codec_type == FFmpeg.CodecType.CODEC_TYPE_VIDEO)
- {
- videoCodecCtx = codec;
- videoStreamIndex = i;
- pCodecContext = stream.codec;
- }
- }
- if (videoCodecCtx == null)
- {
- Console.WriteLine("Video stream not found in file");
- return;
- }
- IntPtr pVideoCodec = FFmpeg.avcodec_find_decoder(videoCodecCtx.Value.codec_id);
- if (pVideoCodec == IntPtr.Zero)
- {
- Console.WriteLine("couldn't find codec");
- return;
- }
- res = FFmpeg.avcodec_open(pCodecContext, pVideoCodec );
- if (!checkResult(res, "Can't open codec")) return;
- IntPtr pFrame = FFmpeg.avcodec_alloc_frame();
- IntPtr pFrameRgb = FFmpeg.avcodec_alloc_frame();
- FFmpeg.AVCodecContext ctx = videoCodecCtx.Value;
- IntPtr pPacket = Marshal.AllocHGlobal(56); //new FFmpeg.AVPacket()
- //-----------------------------------------------------------------------------------------------------------------------------
- //Read frames and decode it until get full picture and save it to file
- int counter = 0;
- while (FFmpeg.av_read_frame(pFormatContext, pPacket) >= 0)
- {
- FFmpeg.AVPacket packet = (FFmpeg.AVPacket) Marshal.PtrToStructure(pPacket, typeof(FFmpeg.AVPacket));
- int finished = 0;
- FFmpeg.avcodec_decode_video(pCodecContext, pFrame, ref finished, packet.data, packet.size);
- if (packet.stream_index == videoStreamIndex)
- {
- if (finished!=0)
- {
- if (++counter <= 25)
- {
- Bitmap bmp = new Bitmap(ctx.width, ctx.height, PixelFormat.Format24bppRgb);
- BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
- FFmpeg.avpicture_fill(pFrameRgb, bd.Scan0, (int)FFmpeg.PixelFormat.PIX_FMT_RGB24, ctx.width, ctx.height);
- IntPtr swsContext = new IntPtr();
- 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);
- if (swsContext == IntPtr.Zero)
- {
- Console.WriteLine("Not able to get libswscale context");
- return;
- }
- FFmpeg.AVFrame src = (FFmpeg.AVFrame)Marshal.PtrToStructure(pFrame, typeof(FFmpeg.AVFrame));
- FFmpeg.AVFrame dst = (FFmpeg.AVFrame)Marshal.PtrToStructure(pFrameRgb, typeof(FFmpeg.AVFrame));
- //convert frame to selected format, this method used instead of deprecated img_convert
- FFmpeg.sws_scale(swsContext, src.data, src.linesize, 0, ctx.height, dst.data, dst.linesize);
- bmp.UnlockBits(bd);
- bmp.Save(String.Format("Frame{0}.png",counter));
- }
- }
- }
- }
- //Release used structures
- Marshal.FreeHGlobal(pPacket);
- FFmpeg.av_free(pFrame);
- FFmpeg.av_free(pFrameRgb);
- FFmpeg.avcodec_close(pCodecContext);
- FFmpeg.av_close_input_file(pFormatContext);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement