首页 > 其他分享 >FFmpeg:视频帧格式转化(sws_scale)(参考scaling_video.c)

FFmpeg:视频帧格式转化(sws_scale)(参考scaling_video.c)

时间:2023-01-14 19:56:17浏览次数:40  
标签:src scale FFmpeg dst pix video sws data fmt

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

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

标签:src,scale,FFmpeg,dst,pix,video,sws,data,fmt
From: https://www.cnblogs.com/michong2022/p/17052426.html

相关文章

  • FFmpeg:视频解码(FFmpeg 5.x 新API)(参考decode_video.c)
    如果不是特别熟悉C/C++,又要使用FFmpeg.API处理一些简单的音视频业务,那么可以使用org.bytedeco:ffmpeg-platform,下面记录一下使用ffmpeg-platform视频解码的方法。1.代码......
  • FFmpeg:音视频封装(含格式转化、重采样)(参考muxing.c)
    如果不是特别熟悉C/C++,又要使用FFmpeg.API处理一些简单的音视频业务,那么可以使用org.bytedeco:ffmpeg-platform,下面记录一下使用ffmpeg-platform获取封装音视频数据(含格式......
  • 使用ffmpeg合并多个ts文件
    使用ffmpeg合并多个ts文件需求来源在海康的行车记录仪上录制的视频为每分钟一个ts文件,在手机上最多也就可以设置为3分钟,并且在APP只能一下导出5个文件,所以我就将内存卡拔......
  • uniapp裁剪视频使用ffmpeg
    <template><view><button@click="cutVideo">CutVideo</button></view></template><script>importffmpegfrom'ffmpeg.js'exportdefault{method......
  • 【每日一读】Large-Scale Nodes Classification With Deep Aggregation Network
    目录​​简介​​​​论文简介​​​​Abstract​​​​1INTRODUCTION​​​​2RELATEDWORKSANDMOTIVATIONS​​​​2.1PrimaryDefinition​​​​2.2Neighborhoo......
  • Zabbix 5.0 使用TimescaleDB数据库
    Zabbix5.0版本支持TimescaleDB作为后端数据库,可提供数据自动分区、自动数据清理、数据压缩等特性。概念:hypertable(超表):用于创建表和索引,更改表,插入数据,选择数据的数据......
  • ffmpeg第8篇:使用ffprobe采集文件信息
    1、前言ffprobe是ffmpeg的其中一个模块,主要用于查看文件信息,咱们知道一个MP4文件其实不仅仅包含了音视频数据,还有如元数据等其它信息,但是实际上咱们关心的往往是音视频......
  • FFmpeg-最最强大的视频工具
    FFmpeg-最最强大的视频工具FFmpeg是一个自由软件,它可以用于处理音频和视频数据。它包含了许多命令行工具,可以进行各种不同的媒体处理任务,如音频编解码、视频编解码、转码、......
  • ffmpeg安装图片转视频
     乌班图:ffmpeg图片转视频一键安装命令:aptinstallffmpeg Centos1.在宝塔终端输入:wgethttp://download.bt.cn/install/ext/ffmpeg.sh&&shffmpeg.sh2.......
  • Window下安装ffmpeg
    下载ffmpeg    下载ffmpeg-git-full.7z   ......