使用 ffmpeg 录制视频和音频

Recording video and audio with ffmpeg

提问人:LeXela-ED 提问时间:11/17/2023 最后编辑:genpfaultLeXela-ED 更新时间:11/18/2023 访问量:38

问:

我正在尝试录制配备麦克风的 IP 摄像机。以下是我的简化版本的代码,它没有错误,它确实记录了视频,但只记录了视频;当我播放录制的视频文件时,没有音频!

使用 VLC 播放录制的文件,我注意到编解码器信息中只有流 0 可用。请让我知道我应该怎么做才能录制音频和视频。

代码如下:

int main(int argc, char *argv[])
{
    const char *input_url = "rtsp://admin:[email protected]:554/profile0";
    const char *output_filename = "out.ts";

    AVFormatContext *input_format_context = avformat_alloc_context();
    if(!input_format_context)
    {
        qDebug() << "Could not allocate input format context\n";
        return EXIT_FAILURE;
    }

    if(avformat_open_input(&input_format_context, input_url, NULL, NULL) < 0)
    {
        qDebug() << "Could not open input file";
        return EXIT_FAILURE;
    }

    if(avformat_find_stream_info(input_format_context, NULL) < 0)
    {
        qDebug() << "Could not find stream information";
        return EXIT_FAILURE;
    }

    int video_stream_index = -1;
    int audio_stream_index = -1;

    for(int i = 0; i < input_format_context->nb_streams; i++)
    {
        if(input_format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
            video_stream_index = i;
        else if(input_format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
            audio_stream_index = i;
    }

    if(video_stream_index == -1)
    {
        qDebug() << "No video stream found in the input file";
        return EXIT_FAILURE;
    }

    AVFormatContext *output_format_context = NULL;
    if(avformat_alloc_output_context2(&output_format_context, NULL, NULL, output_filename) < 0)
    {
        qDebug() << "Could not allocate output format context";
        return EXIT_FAILURE;
    }

    // Add video stream to the media file
    AVStream *video_stream = avformat_new_stream(output_format_context, NULL);
    if(!video_stream)
    {
        qDebug() << "Could not allocate video stream";
        return EXIT_FAILURE;
    }

    if(avcodec_parameters_copy(video_stream->codecpar, input_format_context->streams[video_stream_index]->codecpar) < 0)
    {
        qDebug() << "Failed to copy video stream parameters";
        return EXIT_FAILURE;
    }

    AVStream *audio_stream = NULL;
    if(audio_stream_index != -1)
    {
        // Add audio stream to the media file
        audio_stream = avformat_new_stream(output_format_context, NULL);
        if(!audio_stream)
        {
            qDebug() << "Could not allocate audio stream";
            return EXIT_FAILURE;
        }

        if(avcodec_parameters_copy(audio_stream->codecpar, input_format_context->streams[audio_stream_index]->codecpar) < 0)
        {
            qDebug() << "Failed to copy audio stream parameters";
            return EXIT_FAILURE;
        }
    }

    if(!(output_format_context->oformat->flags & AVFMT_NOFILE))
    {
        if(avio_open(&output_format_context->pb, output_filename, AVIO_FLAG_WRITE) < 0)
        {
            qDebug() << "Could not open output file";
            return EXIT_FAILURE;
        }
    }

    if(avformat_write_header(output_format_context, NULL) < 0)
    {
        qDebug() << "Error occurred when writing output file header";
        return EXIT_FAILURE;
    }

    AVPacket packet;
    while(av_read_frame(input_format_context, &packet) >= 0)
    {
        AVStream *in_stream = input_format_context->streams[packet.stream_index];
        AVStream *out_stream = output_format_context->streams[packet.stream_index];

        packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, static_cast<AVRounding>(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, static_cast<AVRounding>(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
        packet.pos = -1;

        if(av_interleaved_write_frame(output_format_context, &packet) < 0)
        {
            qDebug() << "Error muxing packet";
            break;
        }

        av_packet_unref(&packet);
    }

    av_write_trailer(output_format_context);

    if(!(output_format_context->oformat->flags & AVFMT_NOFILE))
    {
        avio_closep(&output_format_context->pb);
    }

    avformat_free_context(input_format_context);
    avformat_free_context(output_format_context);

    return EXIT_SUCCESS;
}
C++ 音频 视频 ffmpeg rtsp

评论


答: 暂无答案