IOL0ol1 / EmguFFmpeg

A FFmpeg.AutoGen Warpper Library.
MIT License
73 stars 15 forks source link

FFmpeg error [-1] Operation not permitted #2

Open sbabich opened 4 years ago

sbabich commented 4 years ago

Hi!

I try to get video from BlackMagic. But if I restart record process I am getting "FFmpeg error [-1] Operation not permitted"

What am I doing wrong ?

using (var writer = new MediaWriter(outputFileName, new OutFormat("mp4"))) using (var reader0 = new MediaReader(deviceVInput, formatInput)) { var videoStream = reader0.First( => .Codec.Type == AVMediaType.AVMEDIA_TYPE_VIDEO);

                    // init filter source
                    int height = videoStream.Codec.AVCodecContext.height;
                    int width = videoStream.Codec.AVCodecContext.width;
                    int format = (int)videoStream.Codec.AVCodecContext.pix_fmt;
                    var time_base = videoStream.TimeBase;
                    var sample_aspect_ratio = videoStream.Codec.AVCodecContext.sample_aspect_ratio;
                    var r = videoStream.Stream.avg_frame_rate;
                    var fps = Convert.ToDouble(r.num) / Convert.ToDouble(r.den);
                    var frameDuration = 1000 / fps;

                    // add video stream
                    var streamV = MediaEncode.CreateVideoEncode(
                        codecId, 0,
                        width,
                        height,
                        (int)fps,
                        bitrate,
                        opfmt);

                    // init video frame format converter by dstcodec
                    var outStrmV = writer.AddStream(streamV);
                    var pixelConverter = new PixelConverter(outStrmV.Codec);

                    // init
                    writer.Initialize();

                    var capture = false;

                    // FPS control
                    var frames_index = 0;
                    var frames_count = 0;
                    var th2_started = false;

                    var timer = new Stopwatch();
                    _timer.Start();

                    var startPts = 0L;
                    var nullPts = 0L;

                    var filterGraph = new MediaFilterGraph();
                    filterGraph
                        .AddVideoSrcFilter(
                            new MediaFilter(MediaFilter.VideoSources.Buffer),
                            width,
                            height,
                            (AVPixelFormat)format,
                            time_base,
                            sample_aspect_ratio,
                            new AVRational())
                        .LinkTo(0,
                            filterGraph
                                .AddFilter(
                                    new MediaFilter("yadif")))
                        .LinkTo(0,
                            filterGraph
                                .AddVideoSinkFilter(
                                    new MediaFilter(MediaFilter.VideoSinks.Buffersink)));

                    filterGraph.Initialize();

                    try
                    {                            
                            foreach (var srcPacket in reader0.ReadPacket())
                            {
                                foreach (var srcFrame in videoStream.ReadFrame(srcPacket))
                                {
                                    filterGraph.Inputs.First().WriteFrame(srcFrame);

                                    foreach (var filterFrame in filterGraph.Outputs.First().ReadFrame())
                                    {
                                        // Recording to a video file
                                        var curPts = (long)(timer.Elapsed.TotalSeconds * fps);
                                        var ts = new Stopwatch();
                                        ts.Start();

                                        using (var dstMat = filterFrame.ToMat())
                                        using (var g2 = Graphics.FromImage(dstMat.Bitmap))
                                        {
                                            g2.DrawString(timer.Elapsed.ToString(), font, brush, new PointF(50, 50));
                                            using (var dstFrame = dstMat.ToVideoFrame(opfmt))
                                            {
                                                frames_count++;
                                                var pts = (srcFrame.Pts - nullPts) / 1000000L;

                                                dstFrame.Pts = curPts;

                                                var d = srcFrame.Pts - nullPts;
                                                var fpsr = 0.0;
                                                if (timer.Elapsed.TotalSeconds > 0)
                                                    fpsr = (double) frames_count / timer.Elapsed.TotalSeconds;

                                                Console.WriteLine(
                                                    $@"PTS {startPts} {curPts} {dstFrame.Pts} = {d} fps={fpsr}");
                                                startPts += 1;

                                                try
                                                {
                                                    foreach (var dstPacket in writer[0].WriteFrame(dstFrame))
                                                    {
                                                        writer.WritePacket(dstPacket);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Console.WriteLine(e);
                                                    throw;
                                                }
                                            }
                                        }

                                        Console.WriteLine($@"Write frame duration {ts.ElapsedMilliseconds}ms");
                                    }
                                }
                            }

                            timer.Stop();
                            curPts2 = (long)(timer.Elapsed.TotalSeconds * fps);
                            Console.WriteLine($@"t1={frames_count} t2={curPts2}");
                    }
                    finally
                    {
                        writer.FlushMuxer();
                        GC.SuppressFinalize(reader0);
                        GC.SuppressFinalize(writer);
                    }

Please help me if you can. Thanks!

IOL0ol1 commented 4 years ago

Hi!

I try to get video from BlackMagic. But if I restart record process I am getting "FFmpeg error [-1] Operation not permitted"

What am I doing wrong ?

using (var writer = new MediaWriter(outputFileName, new OutFormat("mp4"))) using (var reader0 = new MediaReader(deviceVInput, formatInput)) { var videoStream = reader0.First( => .Codec.Type == AVMediaType.AVMEDIA_TYPE_VIDEO);

                    // init filter source
                    int height = videoStream.Codec.AVCodecContext.height;
                    int width = videoStream.Codec.AVCodecContext.width;
                    int format = (int)videoStream.Codec.AVCodecContext.pix_fmt;
                    var time_base = videoStream.TimeBase;
                    var sample_aspect_ratio = videoStream.Codec.AVCodecContext.sample_aspect_ratio;
                    var r = videoStream.Stream.avg_frame_rate;
                    var fps = Convert.ToDouble(r.num) / Convert.ToDouble(r.den);
                    var frameDuration = 1000 / fps;

                    // add video stream
                    var streamV = MediaEncode.CreateVideoEncode(
                        codecId, 0,
                        width,
                        height,
                        (int)fps,
                        bitrate,
                        opfmt);

                    // init video frame format converter by dstcodec
                    var outStrmV = writer.AddStream(streamV);
                    var pixelConverter = new PixelConverter(outStrmV.Codec);

                    // init
                    writer.Initialize();

                    var capture = false;

                    // FPS control
                    var frames_index = 0;
                    var frames_count = 0;
                    var th2_started = false;

                    var timer = new Stopwatch();
                    _timer.Start();

                    var startPts = 0L;
                    var nullPts = 0L;

                    var filterGraph = new MediaFilterGraph();
                    filterGraph
                        .AddVideoSrcFilter(
                            new MediaFilter(MediaFilter.VideoSources.Buffer),
                            width,
                            height,
                            (AVPixelFormat)format,
                            time_base,
                            sample_aspect_ratio,
                            new AVRational())
                        .LinkTo(0,
                            filterGraph
                                .AddFilter(
                                    new MediaFilter("yadif")))
                        .LinkTo(0,
                            filterGraph
                                .AddVideoSinkFilter(
                                    new MediaFilter(MediaFilter.VideoSinks.Buffersink)));

                    filterGraph.Initialize();

                    try
                    {                            
                            foreach (var srcPacket in reader0.ReadPacket())
                            {
                                foreach (var srcFrame in videoStream.ReadFrame(srcPacket))
                                {
                                    filterGraph.Inputs.First().WriteFrame(srcFrame);

                                    foreach (var filterFrame in filterGraph.Outputs.First().ReadFrame())
                                    {
                                        // Recording to a video file
                                        var curPts = (long)(timer.Elapsed.TotalSeconds * fps);
                                        var ts = new Stopwatch();
                                        ts.Start();

                                        using (var dstMat = filterFrame.ToMat())
                                        using (var g2 = Graphics.FromImage(dstMat.Bitmap))
                                        {
                                            g2.DrawString(timer.Elapsed.ToString(), font, brush, new PointF(50, 50));
                                            using (var dstFrame = dstMat.ToVideoFrame(opfmt))
                                            {
                                                frames_count++;
                                                var pts = (srcFrame.Pts - nullPts) / 1000000L;

                                                dstFrame.Pts = curPts;

                                                var d = srcFrame.Pts - nullPts;
                                                var fpsr = 0.0;
                                                if (timer.Elapsed.TotalSeconds > 0)
                                                    fpsr = (double) frames_count / timer.Elapsed.TotalSeconds;

                                                Console.WriteLine(
                                                    $@"PTS {startPts} {curPts} {dstFrame.Pts} = {d} fps={fpsr}");
                                                startPts += 1;

                                                try
                                                {
                                                    foreach (var dstPacket in writer[0].WriteFrame(dstFrame))
                                                    {
                                                        writer.WritePacket(dstPacket);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Console.WriteLine(e);
                                                    throw;
                                                }
                                            }
                                        }

                                        Console.WriteLine($@"Write frame duration {ts.ElapsedMilliseconds}ms");
                                    }
                                }
                            }

                            timer.Stop();
                            curPts2 = (long)(timer.Elapsed.TotalSeconds * fps);
                            Console.WriteLine($@"t1={frames_count} t2={curPts2}");
                    }
                    finally
                    {
                        writer.FlushMuxer();
                        GC.SuppressFinalize(reader0);
                        GC.SuppressFinalize(writer);
                    }

Please help me if you can. Thanks!

can you paste more detailed information? include