Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何把图片连续帧转成视频推流出去 #20

Open
sunkunGitHub opened this issue May 17, 2024 · 1 comment
Open

如何把图片连续帧转成视频推流出去 #20

sunkunGitHub opened this issue May 17, 2024 · 1 comment

Comments

@sunkunGitHub
Copy link

请问如何把图片连续帧转成视频推流出去,完全不知道要怎么弄

@sunkunGitHub
Copy link
Author

我用如下代码已实现推流,但是仍有几个问题
1、无法推送rtsp的流,提示Protocol not found 协议不存在
2、推出去的流使用vlc播放时播放读秒每次增加3-4秒,画面也不更新

private static void MakeMp4(Codec codec, int width, int height, int frameCount = 30)

    {

        using FormatContext infc = FormatContext.OpenInputUrl("rtsp://admin:1234@10.36.3.92");

        infc.LoadStreamInfo();

        MediaStream videoStream = infc.GetVideoStream();



        using CodecContext videoDecoder = new(Codec.FindDecoderById(videoStream.Codecpar!.CodecId));

        videoDecoder.FillParameters(videoStream.Codecpar!);

        videoDecoder.Open();


        using FormatContext fc = FormatContext.AllocOutput(formatName: "flv");

        fc.VideoCodec = codec;

        MediaStream vstream = fc.NewStream(fc.VideoCodec);

        using CodecContext vcodec = new CodecContext(fc.VideoCodec)

        {

            Width = width,

            Height = height,

            TimeBase = new AVRational(1, 1000),

            PixelFormat = AVPixelFormat.Yuv420p,

            Flags = AV_CODEC_FLAG.GlobalHeader,

        };

        vcodec.Open(fc.VideoCodec, new MediaDictionary

        {

            ["preset"] = "ultrafast"

        });

        vstream.Codecpar!.CopyFrom(vcodec);



        using IOContext io = IOContext.OpenWrite("rtmp://127.0.0.1:1935/live/1");

        fc.Pb = io;

        fc.WriteHeader();

        foreach (Packet packet in infc.ReadPackets(videoStream.Index))

        {

            try

            {

                packet.RescaleTimestamp(vcodec.TimeBase, vstream.TimeBase);

                packet.StreamIndex = vstream.Index;

                fc.InterleavedWritePacket(packet);

            }

            finally

            {

                packet.Unref();

            }

        }

        fc.WriteTrailer();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant