ffmpeg+qsv 硬编码成yuv420p的数据,给些代码示例
时间: 2024-02-12 09:05:34 浏览: 163
这里提供一个使用FFmpeg和QSV硬编码器将视频文件编码为YUV420P格式数据的C++代码示例:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/pixdesc.h>
#include <libavutil/opt.h>
}
int main(int argc, char* argv[])
{
av_register_all();
AVFormatContext* input_ctx = NULL;
int ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to open input file: %s\n", argv[1]);
return 1;
}
ret = avformat_find_stream_info(input_ctx, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to find stream information\n");
return 1;
}
int video_stream_idx = -1;
for (int i = 0; i < input_ctx->nb_streams; i++) {
if (input_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_idx = i;
break;
}
}
if (video_stream_idx < 0) {
fprintf(stderr, "No video stream found\n");
return 1;
}
AVCodec* codec = avcodec_find_decoder(input_ctx->streams[video_stream_idx]->codecpar->codec_id);
if (!codec) {
fprintf(stderr, "Unsupported codec!\n");
return 1;
}
AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
fprintf(stderr, "Failed to allocate codec context\n");
return 1;
}
ret = avcodec_parameters_to_context(codec_ctx, input_ctx->streams[video_stream_idx]->codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters to context\n");
return 1;
}
ret = avcodec_open2(codec_ctx, codec, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to open codec\n");
return 1;
}
AVFrame* frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Failed to allocate frame\n");
return 1;
}
AVPacket packet;
av_init_packet(&packet);
AVCodec* enc_codec = avcodec_find_encoder_by_name("h264_qsv");
if (!enc_codec) {
fprintf(stderr, "Failed to find QSV encoder\n");
return 1;
}
AVCodecContext* enc_ctx = avcodec_alloc_context3(enc_codec);
if (!enc_ctx) {
fprintf(stderr, "Failed to allocate encoder context\n");
return 1;
}
enc_ctx->width = codec_ctx->width;
enc_ctx->height = codec_ctx->height;
enc_ctx->time_base = codec_ctx->time_base;
enc_ctx->framerate = codec_ctx->framerate;
enc_ctx->pix_fmt = AV_PIX_FMT_NV12; // set output pixel format to NV12
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
ret = avcodec_open2(enc_ctx, enc_codec, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to open encoder\n");
return 1;
}
FILE* output_file = fopen("output.yuv", "wb");
if (!output_file) {
fprintf(stderr, "Failed to open output file\n");
return 1;
}
while (av_read_frame(input_ctx, &packet) >= 0) {
if (packet.stream_index != video_stream_idx) {
av_packet_unref(&packet);
continue;
}
ret = avcodec_send_packet(codec_ctx, &packet);
if (ret < 0) {
fprintf(stderr, "Failed to send packet to decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
else if (ret < 0) {
fprintf(stderr, "Failed to receive frame from decoder\n");
goto end;
}
AVFrame* enc_frame = av_frame_alloc();
if (!enc_frame) {
fprintf(stderr, "Failed to allocate encoder frame\n");
goto end;
}
enc_frame->format = enc_ctx->pix_fmt;
enc_frame->width = enc_ctx->width;
enc_frame->height = enc_ctx->height;
ret = av_frame_get_buffer(enc_frame, 32);
if (ret < 0) {
fprintf(stderr, "Failed to allocate encoder frame buffer\n");
av_frame_free(&enc_frame);
goto end;
}
ret = av_hwframe_transfer_data(enc_frame, frame, 0);
if (ret < 0) {
fprintf(stderr, "Failed to transfer frame data to encoder\n");
av_frame_free(&enc_frame);
goto end;
}
AVPacket enc_packet;
av_init_packet(&enc_packet);
ret = avcodec_send_frame(enc_ctx, enc_frame);
if (ret < 0) {
fprintf(stderr, "Failed to send frame to encoder\n");
av_packet_unref(&enc_packet);
av_frame_free(&enc_frame);
goto end;
}
while (ret >= 0) {
ret = avcodec_receive_packet(enc_ctx, &enc_packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
else if (ret < 0) {
fprintf(stderr, "Failed to receive packet from encoder\n");
av_packet_unref(&enc_packet);
av_frame_free(&enc_frame);
goto end;
}
fwrite(enc_packet.data, 1, enc_packet.size, output_file);
av_packet_unref(&enc_packet);
}
av_frame_free(&enc_frame);
}
av_packet_unref(&packet);
}
end:
fclose(output_file);
avcodec_free_context(&enc_ctx);
avcodec_free_context(&codec_ctx);
avformat_close_input(&input_ctx);
av_frame_free(&frame);
return 0;
}
```
这个示例代码使用H.264 QSV硬编码器将输入文件编码为YUV420P格式数据,并将结果保存到输出文件output.yuv中。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)