ffmpeg 将内存中的H264跟PCM 数据流合成多媒体文件demo

27 篇文章 198 订阅 ¥9.90 ¥99.00
40 篇文章 14 订阅
11 篇文章 0 订阅
/*
 * Copyright (c) 2003 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTI
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Visual Studio 使用 FFmpeg内存的 H.264 数据保存为 TS 文件,您需要将 FFmpeg 库添加到您的项目,并使用 C++ 代码编写文件保存功能。以下是一个简单的示例程序,它将内存的 H.264 数据保存为 MPEG-TS 文件: ```cpp extern "C" { #include <libavformat/avformat.h> #include <libavutil/opt.h> } int save_as_ts(unsigned char* h264_data, int h264_size, const char* output_file) { AVFormatContext* output_format_ctx = NULL; AVOutputFormat* output_format = NULL; AVStream* video_stream = NULL; AVPacket pkt; int ret; av_register_all(); // Register MPEG-TS output format output_format = av_guess_format("mpegts", NULL, NULL); if (!output_format) { return AVERROR_MUXER_NOT_FOUND; } // Create output format context if ((ret = avformat_alloc_output_context2(&output_format_ctx, output_format, NULL, output_file)) < 0) { return ret; } // Add video stream to output format context video_stream = avformat_new_stream(output_format_ctx, NULL); if (!video_stream) { return AVERROR_UNKNOWN; } // Set video codec parameters video_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; video_stream->codecpar->codec_id = output_format->video_codec; video_stream->codecpar->width = 1280; // replace with your video width video_stream->codecpar->height = 720; // replace with your video height video_stream->codecpar->format = AV_PIX_FMT_YUV420P; // replace with your video pixel format // Open output file for writing if ((ret = avio_open(&output_format_ctx->pb, output_file, AVIO_FLAG_WRITE)) < 0) { return ret; } // Write stream headers if ((ret = avformat_write_header(output_format_ctx, NULL)) < 0) { return ret; } // Write H.264 packets to output file av_init_packet(&pkt); pkt.data = h264_data; pkt.size = h264_size; pkt.pts = AV_NOPTS_VALUE; pkt.dts = AV_NOPTS_VALUE; pkt.duration = 1; pkt.stream_index = video_stream->index; if ((ret = av_interleaved_write_frame(output_format_ctx, &pkt)) < 0) { return ret; } // Write stream trailer av_write_trailer(output_format_ctx); // Close output file avio_close(output_format_ctx->pb); avformat_free_context(output_format_ctx); return 0; } ``` 此代码使用 FFmpeg 库的 AVFormatContext 结构体创建 MPEG-TS 文件,并使用 avformat_new_stream() 函数创建视频流。然后,使用 avio_open() 函数打开输出文件,使用 avformat_write_header() 函数写入文件头,并使用 av_interleaved_write_frame() 函数将 H.264 包写入文件。最后,使用 av_write_trailer() 函数写入文件尾,并使用 avio_close() 函数关闭输出文件。 您可以在 Visual Studio 创建一个 C++ 项目,并将 FFmpeg 库和头文件添加到项目。然后,您可以将此代码添加到您的项目,并调用 save_as_ts() 函数来保存内存的 H.264 数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmbbPdx_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值