使用ffmpeg用gpu解码mkv视频为yuv格式

ffmpeg -vsync 0 -c:v h264_cuvid -i 2528.mkv -f rawvideo $count.yuv


附查看显卡命令:

nvidia-smi



启动多个实例脚本:

#! /bin/sh

COUNT=8

while true
do

  count=`expr $count + 1`
  ffmpeg -vsync 0 -c:v h264_cuvid -i 2528.mkv -f rawvideo $count.yuv &

  if [ $count -ge $COUNT ]
  then
    break
  fi

done

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用c语言和FFmpeg解码视频yuv420p格式并保存文件的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavutil/imgutils.h> #include <libavutil/parseutils.h> #include <libavutil/opt.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> int main(int argc, char *argv[]) { AVFormatContext *fmt_ctx = NULL; int ret; if (argc < 2) { fprintf(stderr, "Usage: %s <input file>\n", argv[0]); exit(1); } ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL); if (ret < 0) { fprintf(stderr, "Could not open input file '%s'\n", argv[1]); exit(1); } ret = avformat_find_stream_info(fmt_ctx, NULL); if (ret < 0) { fprintf(stderr, "Could not find stream information\n"); exit(1); } int video_stream_index = -1; for (int i = 0; i < fmt_ctx->nb_streams; i++) { if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { fprintf(stderr, "Could not find video stream\n"); exit(1); } AVCodecParameters *codecpar = fmt_ctx->streams[video_stream_index]->codecpar; AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } AVCodecContext *codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); exit(1); } ret = avcodec_parameters_to_context(codec_ctx, codecpar); if (ret < 0) { fprintf(stderr, "Could not copy codec parameters to codec context\n"); exit(1); } ret = avcodec_open2(codec_ctx, codec, NULL); if (ret < 0) { fprintf(stderr, "Could not open codec\n"); exit(1); } AVPacket pkt; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; AVFrame *frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate frame\n"); exit(1); } int frame_count = 0; while (av_read_frame(fmt_ctx, &pkt) >= 0) { if (pkt.stream_index == video_stream_index) { ret = avcodec_send_packet(codec_ctx, &pkt); if (ret < 0) { fprintf(stderr, "Error sending packet to decoder\n"); exit(1); } while (ret >= 0) { ret = avcodec_receive_frame(codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error during decoding\n"); exit(1); } char filename[256]; snprintf(filename, sizeof(filename), "frame-%d.yuv", frame_count); FILE *fp = fopen(filename, "wb"); if (!fp) { fprintf(stderr, "Could not open output file '%s'\n", filename); exit(1); } for (int i = 0; i < codec_ctx->height; i++) { fwrite(frame->data[0] + i * frame->linesize[0], 1, codec_ctx->width, fp); } for (int i = 0; i < codec_ctx->height / 2; i++) { fwrite(frame->data[1] + i * frame->linesize[1], 1, codec_ctx->width / 2, fp); } for (int i = 0; i < codec_ctx->height / 2; i++) { fwrite(frame->data[2] + i * frame->linesize[2], 1, codec_ctx->width / 2, fp); } fclose(fp); frame_count++; } } av_packet_unref(&pkt); } avcodec_free_context(&codec_ctx); avformat_close_input(&fmt_ctx); avformat_free_context(fmt_ctx); av_frame_free(&frame); return 0; } ``` 该示例代码从命令行读取一个输入文件名,并使用FFmpeg打开该文件。然后它搜索视频流并使用视频解码解码视频帧。对于每个解码帧,它将像素数据写入一个yuv文件。每个yuv文件的名称都是“frame-N.yuv”,其中N是帧计数器。最后,它释放所有使用的资源并退出。 请注意,此示例代码假定输入文件是可解码视频文件。如果您想处理其他类型的输入文件,请根据需要进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值