gpt4 book ai didi

android - 更改 ffmpeg 输出中的分辨率以在 Android 中显示视频流

转载 作者:行者123 更新时间:2023-12-04 23:03:59 25 4
gpt4 key购买 nike

我正在尝试将相机输出(分辨率:640 x 480)更改为 1024 x 720 并在 Android 屏幕上渲染视频帧。是否可以使用 ffmpeg 和 SDL 库进行此视频转换?如果可以的话有没有ffmepeg 编解码器库中的 API 可以做同样的事情吗?

这是我获得 640 x 480 分辨率输出的代码:

//Registering all the formats:
av_register_all();
AVFormatContext *pFormatCtx=NULL;
int i, videoStream;
AVCodecContext *pCodecCtx=NULL;
AVCodec *pCodec=NULL;
AVFrame *pFrame;
AVPacket packet;
int frameFinished;
SDL_Texture *bmp;
SDL_Renderer *renderer;
SDL_Window *screen;

if (SDL_Init(SDL_INIT_VIDEO)) {
LOGD( "Could not initialize SDL - %s\n", SDL_GetError());
SDL_Quit();
exit(1);
}
LOGD(" SDL Initialized..");

screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 0, 0,
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN);
LOGD("SDL Screen Created ..");

renderer = SDL_CreateRenderer(screen,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
LOGD("Rendering Created...");

bmp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV,SDL_TEXTUREACCESS_STREAMING,640,480);
LOGD("Texture created;");

SDL_RenderSetLogicalSize(renderer,640,480);
// Open video file
if(avformat_open_input(&pFormatCtx,"Filename", NULL,NULL)!=0)
LOGD("Cannot open the File");

pFormatCtx->interrupt_callback.callback = decode_interrupt_cb;

// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx,NULL)<0)
LOGD("Cannot retrive Stream info");
// Couldn't find stream information

videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
if(videoStream==-1)
LOGD("Cannot find Video Stream:");
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
LOGD("Unable to find the decoder");
// Codec not found
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
LOGD("Unable to OPEN Codec");
// Could not open codec

// Allocate video frame
pFrame=avcodec_alloc_frame();

i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,&packet);

// Did we get a video frame?
if(frameFinished) {
//----------------Code for Displaying
SDL_UpdateYUVTexture(bmp, NULL, pFrame->data[0],
pFrame->linesize[0], pFrame->data[1], pFrame->linesize[1],
pFrame->data[2], pFrame->linesize[2]);

retcl = SDL_RenderClear(renderer);
retcopy = SDL_RenderCopy(renderer, bmp, NULL, NULL);
SDL_RenderPresent(renderer);
//-----------------
}
}

// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}

// Free the RGB image
av_free(buffer);

// Free the YUV frame
av_free(pFrame);

// Close the codec
avcodec_close(pCodecCtx);

// Close the video file
avformat_close_input(&pFormatCtx);

return 0;
}

最佳答案

ffmpeg 使用“过滤器图”来应用缩放、裁剪等过滤器。如果您查看 filtering video example您将看到如何以编程方式应用过滤器。如果您可以将该示例集成到您的代码中,那么您所要做的就是更改字符串 - filter_descr - 以将视频缩放到您想要的分辨率。

过滤器的定义方式与独立程序相同。见documentation更多细节。

关于android - 更改 ffmpeg 输出中的分辨率以在 Android 中显示视频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26560192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com