首页 > 编程语言 > 详细

java FFmpeg 图片生成视频

时间:2021-06-21 20:26:39      阅读:15      评论:0      收藏:0      [点我收藏+]

POM

如果版本有问题,就参考这个:http://bytedeco.org/download/

<!--  ffmpeg  -->
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacpp-platform</artifactId>
      <version>1.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>opencv-platform</artifactId>
      <version>4.5.1-1.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>ffmpeg-platform</artifactId>
      <version>4.3.2-1.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>opencv-platform-gpu</artifactId>
      <version>4.5.1-1.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv-platform</artifactId>
      <version>1.5.5</version>
    </dependency>

 

方法:

public static void pic2MovByFfmpeg(String mp4SavePath,String picturesPath, int width, int height) throws FFmpegFrameRecorder.Exception {
        //视频宽高最好是按照常见的视频的宽高  16:9  或者 9:16
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(mp4SavePath, width, height);
        //设置视频编码层模式
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        //设置视频为1帧每秒
        recorder.setFrameRate(1);
        //设置视频图像数据格式
        recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
        recorder.setFormat("mp4");
 
        File file = new File(picturesPath);
        File[] flist = file.listFiles();
        try {
            recorder.start();
            Java2DFrameConverter converter = new Java2DFrameConverter();
            //录制一个22秒的视频
            for (int i = 0; i < flist.length; i++) {
                BufferedImage read = ImageIO.read(flist[i]);
                recorder.record(converter.getFrame(read),avutil.AV_PIX_FMT_RGB32_1);
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //最后一定要结束并释放资源
            recorder.stop();
            recorder.release();
        }
    }

 

测试:

@Test
    public void pic2movByFfmpeg() throws Exception {
        String picPath="C:\\Users\\Tyler\\Postman\\files";
        String videoPath="C:\\Users\\Tyler\\Downloads\\test.mp4";
        FileHelper.pic2MovByFfmpeg(videoPath,picPath,640,480);
    }

 

java FFmpeg 图片生成视频

原文:https://www.cnblogs.com/hanjun0612/p/14915380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!