ffmpeg nv12 转 yuv420p
时间: 2024-03-14 22:42:12 浏览: 19
FFmpeg是一个开源的跨平台音视频处理工具,可以用于处理、转码、编辑和播放各种音视频文件。NV12和YUV420P都是常见的视频像素格式,而NV12转YUV420P是一种常见的像素格式转换操作。
在FFmpeg中,可以使用以下命令将NV12格式的视频转换为YUV420P格式:
```
ffmpeg -i input.nv12 -pix_fmt yuv420p output.yuv
```
其中,`input.nv12`是输入的NV12格式视频文件,`output.yuv`是输出的YUV420P格式视频文件。通过指定`-pix_fmt yuv420p`参数,可以将输出的像素格式设置为YUV420P。
需要注意的是,上述命令只是进行像素格式的转换,并不会对视频进行重新编码。如果需要对视频进行其他处理或者重新编码,可以在命令中添加相应的参数和选项。
相关问题
ffmpeg yuv422转,nv21
你可以使用FFmpeg库来进行YUV422到NV21格式的转换。下面是一个示例命令:
```bash
ffmpeg -s widthxheight -pix_fmt yuv422p -i input.yuv -vf "format=nv21" output.yuv
```
在命令中,你需要替换`width`和`height`为你的输入文件的分辨率,`input.yuv`为输入文件名,`output.yuv`为输出文件名。
请确保你已经安装了FFmpeg,并使用正确的命令行参数来执行转换。
ffmpeg+qsv 硬编码成yuv420p的数据,给些代码示例
这里提供一个使用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中。