赞
踩
出于项目要求,需要识别视频中的声音,转成文字后,以字幕的形式再嵌入视频中。完成项目需要分成三步。第一步需要提取中的视频中的音频,第二步把音频转成文字保存成字幕文件,第三部把字幕嵌入原视频。
本文主要介绍第一步:提取视频中的音频。
ffmpeg -i d:\123.mp4 -vn 123.mp3
# d:123.mp4 源视频
# 123.mp3 导出音频
av_register_all(); avcodec_register_all(); AVFormatContext* ifmt_ctx = NULL; int a_stream_index = -1; AVCodec* a_dec = NULL; AVCodecContext* a_dec_ctx = NULL; AVPacket* pkt; AVFrame* frame; SwrContext* swr_ctx; int ret; ret = avformat_open_input(&ifmt_ctx, m_srcFilePath.toStdString().c_str(), NULL, NULL); if (ret < 0) { return; } avformat_find_stream_info(ifmt_ctx, NULL); a_stream_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &a_dec, 0); if (a_stream_index < 0) { goto __FAIL; } a_dec_ctx = avcodec_alloc_context3(a_dec); a_dec_ctx->pkt_timebase = ifmt_ctx->streams[a_stream_index]->time_base; avcodec_parameters_to_context(a_dec_ctx, ifmt_ctx->streams[a_stream_index]->codecpar); if (!a_dec_ctx) { goto __FAIL; } ret = avcodec_open2(a_dec_ctx, a_dec, NULL); if (ret < 0) { goto __FAIL; } frame = av_frame_alloc(); pkt = av_packet_alloc(); //输出采样格式 int64_t out_ch_layout = AV_CH_LAYOUT_MONO; enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16; int out_sample_rate = OUT_SAMPLE; swr_ctx = swr_alloc_set_opts(NULL, out_ch_layout, out_sample_fmt, out_sample_rate, a_dec_ctx->channels, a_dec_ctx->sample_fmt, a_dec_ctx->sample_rate, 0, NULL); ret = swr_init(swr_ctx); uint8_t* out_buffer = (uint8_t*)av_malloc(OUT_SAMPLE * 2); FILE* fp_pcm = _wfopen(m_destFilePath.toStdWString().c_str(), L"wb");//输出到文件 if (fp_pcm == NULL) { return; } while (1) { ret = av_read_frame(ifmt_ctx, pkt); if (ret < 0) { break; } if (pkt->stream_index != a_stream_index) { printf("%d\n", pkt->size); continue; } ret = avcodec_send_packet(a_dec_ctx, pkt); if (ret < 0) { printf("avcodec_send_packet出错\n"); break; } while (1) { ret = avcodec_receive_frame(a_dec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } int n = swr_convert(swr_ctx, &out_buffer, OUT_SAMPLE * 2, (const uint8_t**)frame->data, frame->nb_samples); //int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0); fwrite(out_buffer, 2, n, fp_pcm); } } while (1) { pkt->data = NULL; pkt->size = 0; ret = avcodec_send_packet(a_dec_ctx, pkt); if (ret < 0) { printf("avcodec_send_packet出错\n"); break; } while (1) { ret = avcodec_receive_frame(a_dec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } int n = swr_convert(swr_ctx, &out_buffer, OUT_SAMPLE * 2, (const uint8_t**)frame->data, frame->nb_samples); //int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0); fwrite(out_buffer, 2, n, fp_pcm); } } __FAIL: fclose(fp_pcm); if (ifmt_ctx) { avformat_close_input(&ifmt_ctx); avformat_free_context(ifmt_ctx); } if (out_buffer) { av_free(out_buffer); } if (frame) { av_frame_free(&frame); } if (pkt) { av_packet_free(&pkt); } if (swr_ctx) { swr_free(&swr_ctx); } if (a_dec_ctx) { avcodec_close(a_dec_ctx); avcodec_free_context(&a_dec_ctx); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。