FFmpeg:视频帧格式转化(sws_scale)(参考scaling_video.c)
如果不是特别熟悉C/C++,又要使用FFmpeg.API处理一些简单的音视频业务,那么可以使用org.bytedeco:ffmpeg-platform,下面记录一下使用ffmpeg-platform视频帧格式转化的方法。
1. 代码实现
视频帧转化在处理视频转码时比较常见,比如要将RGB24转成YUV420P,下面一个将YUV420P的视频帧转成RGB24的例子:
public class ScalingVideo {
public static void scaling_video(String output, int width, int height) throws IOException {
SwsContext sws_ctx = null;
PointerPointer<BytePointer> src_data = new PointerPointer<>(4);
PointerPointer<BytePointer> dst_data = new PointerPointer<>(4);
IntPointer src_linesize = new IntPointer(4);
IntPointer dst_linesize = new IntPointer(4);
try (OutputStream os = new FileOutputStream(output)) {
int src_w = 352, src_h = 288;
int src_pix_fmt = AV_PIX_FMT_YUV420P, dst_pix_fmt = AV_PIX_FMT_RGB24;
sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt, width, height, dst_pix_fmt, SWS_BICUBIC, null, null,
(DoublePointer) null);
if (Objects.isNull(sws_ctx)) {
throw new IOException("sws_getContext error");
}
// allocate source and destination image buffers
int ret = av_image_alloc(src_data, src_linesize, src_w, src_h, src_pix_fmt, 16);
if (ret < 0) {
throw new IOException(ret + ":av_image_alloc error");
}
// buffer is going to be written to rawvideo file, no alignment
ret = av_image_alloc(dst_data, dst_linesize, width, height, dst_pix_fmt, 1);
if (ret < 0) {
throw new IOException(ret + ":av_image_alloc error");
}
byte[] buffer = new byte[ret];
for (int i = 0; i < 100; i++) {
fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
sws_scale(sws_ctx, src_data, src_linesize, 0, src_h, dst_data, dst_linesize);
dst_data.get(BytePointer.class, 0).get(buffer);
os.write(buffer);
}
System.out.printf(
"Scaling succeeded. Play the output file with the command:\n"
+ "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
av_get_pix_fmt_name(dst_pix_fmt).getString(), width, height, output);
} finally {
if (Objects.nonNull(sws_ctx)) {
sws_freeContext(sws_ctx);
}
src_data.close();
src_linesize.close();
dst_data.close();
dst_linesize.close();
}
}
}
2. 结果展示
转化的RGB24视频数据,有使用ffplay播放,效果如下:
播放命令:
ffplay -f rawvideo -pix_fmt rgb24 -video_size 352x288 t.rgb24
欢迎关注微信公众号号:HiIT青年
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理