随笔 - 59,  文章 - 1,  评论 - 0,  阅读 - 24152

如果不是特别熟悉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

完整代码:扫描左侧头像小程序获取,或私信联系。

posted on   $$X$$  阅读(309)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

点击右上角即可分享
微信分享提示