You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将屏幕像素坐标转换为视频本地像素坐标?

问题:FFmpeg读取视频像素坐标匹配与帧数据存储顺序疑问

我使用FFmpeg读取视频文件获取特定像素颜色时,遇到了全屏播放时屏幕像素坐标(x,y)与视频本地像素坐标不匹配的问题,根源是视频分辨率和屏幕分辨率不一致。我尝试过两种转换思路,但均存在问题:

  • 手动测量垂直偏移进行坐标转换
  • 调整SwsContext将目标分辨率设为屏幕分辨率

测试发现视频帧首像素应为黑色,但读取到的像素颜色与ColorSync Utility显示的结果有偏移,怀疑FFmpeg的帧数据是从右到左读取的。以下是简化后的代码:

struct SwsContext *rescaler = NULL;
rescaler = sws_getContext(codec_context->width, codec_context->height, codec_context->pix_fmt, codec_context->width, codec_context->height, AV_PIX_FMT_RGB0, SWS_FAST_BILINEAR, NULL, NULL, 0);

// Get Packets (containers for frames but not guaranteed to have a full frame) and Frames
while (av_read_frame(avformatcontext, packet) >= 0)
{
    
    // determine if packet is video packet
    if (packet->stream_index != video_index)
    {
        continue;
    }
    
    // send packet to decoder
    if (avcodec_send_packet(codec_context, packet) < 0)
    {
        perror("Failed to decode packet");
    }
    
    // get frame from decoder
    int response = avcodec_receive_frame(codec_context, frame);
    if (response == AVERROR(EAGAIN))
    {
        continue;
    }
    else if (response < 0)
    {
        perror("Failed to get frame");
    }
    
    // convert frame to RGB0 colorspace 4 bytes per pixel 1 per channel
    response = sws_scale_frame(rescaler, scaled_frame, frame);
    if(response < 0){
        perror("Failed to change colorspace");
    }
    // get data and write it
    int pixel_number = y*(scaled_frame->linesize[0]/4)+x; // divide by four gets pixel linesize (4 byte per pixel)
    int byte_number = 4*(pixel_number-1); // position of pixel in array
    // start of debugging things
    int temp = scaled_frame->data[0][byte_number]; // R
    int one_after = scaled_frame->data[0][byte_number+1]; // G
    int two_after = scaled_frame->data[0][byte_number+2]; // B
    int als; // where i put the breakpoint
    // end of debugging things
}

核心需求

  1. 求解决全屏场景下,屏幕像素坐标转换为视频本地像素坐标的可靠方法
  2. 确认FFmpeg帧数据的存储顺序

内容的提问来源于stack exchange,提问作者AmLearning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 14:30:56