c++ ffmpeg 录制声音
时间: 2023-11-03 07:59:47 浏览: 198
你可以使用FFmpeg来录制声音。下面是一个使用FFmpeg录制声音的示例命令:
```shell
ffmpeg -f avfoundation -i ":0" output.wav
```
这个命令将从默认音频设备录制声音,并将其保存为output.wav文件。你可以修改输出文件名和路径以适应你的需求。
相关问题
c++ FFmpeg录制视频
以下是使用c++和FFmpeg录制视频的步骤:
1.初始化FFmpeg库并打开输出文件
```c++
av_register_all();
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_file);
if (!ofmt_ctx) {
printf("Could not create output context\n");
return;
}
if (avio_open(&ofmt_ctx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
printf("Could not open output file '%s'", out_file);
return;
}
```
2.添加视频流和音频流
```c++
AVStream *video_st = NULL;
AVStream *audio_st = NULL;
video_st = avformat_new_stream(ofmt_ctx, NULL);
audio_st = avformat_new_stream(ofmt_ctx, NULL);
if (!video_st || !audio_st) {
printf("Failed allocating output stream\n");
return;
}
```
3.设置视频流和音频流的编码器参数
```c++
AVCodecContext *video_enc_ctx = NULL;
AVCodecContext *audio_enc_ctx = NULL;
video_enc_ctx = video_st->codec;
audio_enc_ctx = audio_st->codec;
video_enc_ctx->codec_id = ofmt_ctx->oformat->video_codec;
audio_enc_ctx->codec_id = ofmt_ctx->oformat->audio_codec;
video_enc_ctx->bit_rate = 400000;
audio_enc_ctx->bit_rate = 64000;
video_enc_ctx->width = 640;
video_enc_ctx->height = 480;
video_enc_ctx->time_base = (AVRational){1, 25};
audio_enc_ctx->sample_rate = 44100;
audio_enc_ctx->channels = 2;
audio_enc_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
audio_enc_ctx->time_base = (AVRational){1, audio_enc_ctx->sample_rate};
```
4.打开视频流和音频流的编码器
```c++
AVCodec *video_enc = NULL;
AVCodec *audio_enc = NULL;
video_enc = avcodec_find_encoder(video_enc_ctx->codec_id);
audio_enc = avcodec_find_encoder(audio_enc_ctx->codec_id);
if (!video_enc || !audio_enc) {
printf("Codec not found\n");
return;
}
if (avcodec_open2(video_enc_ctx, video_enc, NULL) < 0 || avcodec_open2(audio_enc_ctx, audio_enc, NULL) < 0) {
printf("Could not open codec\n");
return;
}
```
5.写入文件头
```c++
avformat_write_header(ofmt_ctx, NULL);
```
6.循环读取视频帧和音频帧,编码并写入输出文件
```c++
AVFrame *frame = av_frame_alloc();
AVPacket pkt = {0};
while (true) {
// 读取视频帧和音频帧
AVFrame *video_frame = read_video_frame();
AVFrame *audio_frame = read_audio_frame();
if (!video_frame && !audio_frame) {
break;
}
// 编码视频帧和音频帧
if (video_frame) {
encode_video_frame(video_frame, &pkt);
}
if (audio_frame) {
encode_audio_frame(audio_frame, &pkt);
}
// 写入输出文件
av_interleaved_write_frame(ofmt_ctx, &pkt);
av_packet_unref(&pkt);
}
```
7.写入文件尾
```c++
av_write_trailer(ofmt_ctx);
```
完整代码请参考上述提供的引用。
C++ ffmpeg录制音频
要使用C++和FFmpeg来录制音频,你需要使用FFmpeg库中的AVFormatContext和AVCodecContext结构体。下面是一个示例代码,可以进行音频录制:
```
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/opt.h>
#include <libavutil/time.h>
}
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
volatile int stop = 0;
static void sigterm_handler(int sig)
{
stop = 1;
}
int main(int argc, char **argv)
{
AVFormatContext *oc;
AVCodecContext *audio_ctx;
AVStream *audio_st;
AVCodec *audio_codec;
AVFrame *audio_frame;
uint8_t audio_inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
uint8_t *audio_data;
int audio_data_size;
int ret;
int fd;
int64_t start_time;
if (argc != 2) {
fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
exit(1);
}
// 打开输出文件
if (avformat_alloc_output_context2(&oc, NULL, NULL, argv[1]) < 0) {
fprintf(stderr, "Could not allocate output context\n");
return -1;
}
// 打开音频输入设备
if ((fd = open("/dev/dsp", O_RDONLY)) < 0) {
fprintf(stderr, "Could not open audio device\n");
return -1;
}
// 创建音频流
audio_st = avformat_new_stream(oc, NULL);
if (!audio_st) {
fprintf(stderr, "Could not create audio stream\n");
return -1;
}
// 设置音频编码器
audio_codec = avcodec_find_encoder(oc->audio_codec_id);
if (!audio_codec) {
fprintf(stderr, "Could not find encoder for %s\n", avcodec_get_name(oc->audio_codec_id));
return -1;
}
audio_ctx = avcodec_alloc_context3(audio_codec);
if (!audio_ctx) {
fprintf(stderr, "Could not allocate audio codec context\n");
return -1;
}
audio_ctx->sample_fmt = audio_codec->sample_fmts[0];
audio_ctx->bit_rate = 64000;
audio_ctx->sample_rate = 44100;
audio_ctx->channels = 2;
audio_ctx->channel_layout = av_get_default_channel_layout(2);
audio_st->time_base = (AVRational){1, audio_ctx->sample_rate};
if (avcodec_open2(audio_ctx, audio_codec, NULL) < 0) {
fprintf(stderr, "Could not open audio codec\n");
return -1;
}
avcodec_parameters_from_context(audio_st->codecpar, audio_ctx);
// 初始化音频帧
audio_frame = av_frame_alloc();
if (!audio_frame) {
fprintf(stderr, "Could not allocate audio frame\n");
return -1;
}
audio_frame->format = audio_ctx->sample_fmt;
audio_frame->channel_layout = audio_ctx->channel_layout;
audio_frame->sample_rate = audio_ctx->sample_rate;
audio_frame->nb_samples = audio_ctx->frame_size;
if (av_frame_get_buffer(audio_frame, 0) < 0) {
fprintf(stderr, "Could not allocate audio data buffers\n");
return -1;
}
// 开始录制
av_dump_format(oc, 0, argv[1], 1);
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
if (avio_open(&oc->pb, argv[1], AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "Could not open output file '%s'\n", argv[1]);
return -1;
}
}
if (avformat_write_header(oc, NULL) < 0) {
fprintf(stderr, "Error occurred when writing header to output file\n");
return -1;
}
start_time = av_gettime_relative();
// 信号处理程序
signal(SIGINT, sigterm_handler);
signal(SIGTERM, sigterm_handler);
// 录制音频
while (!stop) {
audio_data_size = read(fd, audio_inbuf, AUDIO_INBUF_SIZE);
if (audio_data_size <= 0) {
break;
}
audio_data = audio_inbuf;
while (audio_data_size > 0) {
int len1, got_frame;
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
len1 = avcodec_decode_audio4(audio_ctx, audio_frame, &got_frame, &pkt);
if (len1 < 0) {
fprintf(stderr, "Error decoding audio frame\n");
break;
}
if (got_frame) {
AVRational tb = (AVRational){1, audio_ctx->sample_rate};
int64_t pts = av_rescale_q(audio_frame->pts, tb, audio_st->time_base);
int64_t now = av_gettime_relative() - start_time;
if (pts > now) {
av_usleep(pts - now);
}
audio_frame->pts = av_rescale_q(now, (AVRational){1, AV_TIME_BASE}, audio_st->time_base);
ret = avcodec_send_frame(audio_ctx, audio_frame);
if (ret < 0) {
fprintf(stderr, "Error sending audio frame\n");
break;
}
while (ret >= 0) {
AVPacket pkt_out;
av_init_packet(&pkt_out);
pkt_out.data = NULL;
pkt_out.size = 0;
ret = avcodec_receive_packet(audio_ctx, &pkt_out);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
break;
}
pkt_out.stream_index = audio_st->index;
pkt_out.pts = av_rescale_q(pkt_out.pts, audio_st->time_base, oc->streams[pkt_out.stream_index]->time_base);
pkt_out.dts = av_rescale_q(pkt_out.dts, audio_st->time_base, oc->streams[pkt_out.stream_index]->time_base);
pkt_out.duration = av_rescale_q(pkt_out.duration, audio_st->time_base, oc->streams[pkt_out.stream_index]->time_base);
ret = av_interleaved_write_frame(oc, &pkt_out);
if (ret < 0) {
fprintf(stderr, "Error writing audio frame\n");
break;
}
}
}
audio_data += len1;
audio_data_size -= len1;
if (pkt.data) {
av_packet_unref(&pkt);
}
}
}
// 结束录制
av_write_trailer(oc);
if (oc && !(oc->oformat->flags & AVFMT_NOFILE)) {
avio_closep(&oc->pb);
}
avcodec_free_context(&audio_ctx);
av_frame_free(&audio_frame);
avformat_free_context(oc);
return 0;
}
```
该示例程序会打开Linux系统上的 `/dev/dsp` 音频设备来进行录制,你可以根据需要修改打开的设备路径。录制的音频编码格式为MP3,可以根据需要修改相关参数。你需要将上述代码保存到一个C++源文件中,并使用FFmpeg库进行编译链接。
阅读全文