欢迎来到淼淼之森的个人小站。  交流请加我微信好友: studyjava。  也欢迎关注同名公众号:Java学习之道

JavaCV开发详解之2:截取推流服务器视频的某帧或本地视频的某帧

  |   0 评论   |   0 浏览

引入依赖

<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.5.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.3</version>
        </dependency>

代码实现

package com.java.mmzsblog;
 
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.opencv_core.IplImage;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
/**
 * @author :created by mmzsblog.cn
 * @description: javaCV开发详解之2:截取推流服务器视频的某帧或本地视频的某帧
 * @date :created at 2020/07/07 10:57
 */
public class VideoTool {
 
    public static void main(String[] args) throws Exception {
 
        // randomGrabberFFmpegImage("http://192.168.0.11/image/00/05/test.mp4", "D:\\test", "test2");
//        randomGrabberFFmpegImage("D:\\1.mp4", "D:\\test", "test");
        randomGrabberFFmpegImage("rtmp://192.168.0.11:1935/live/123456", "D:\\test", "test");
        //randomGrabberFFmpegImage("C:/Users\\Administrator\\Desktop\\VID_20171229_162251.mp4", "G:\\test", "111");
    }
 
    public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName)
            throws Exception {
        //创建视频帧抓取工具
        FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
        ff.start();
        //获取旋转角度信息(90度)
        String rotate = ff.getVideoMetadata("rotate");
        //帧
        Frame f;
        //i可以控制获取第几帧
        int i = 0;
        while (i < 100) {
            //一帧一帧去抓取视频图片,ff.grabImage();每次抓取下一帧
            f = ff.grabImage();
            IplImage src = null;
            //旋转图像并输出第 i 帧(i是4的倍数)
            if (i % 4 == 0) {
                //手机录的视频有旋转角度,需要旋转处理
                if (null != rotate && rotate.length() > 1) {
                    OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
                    src = converter.convert(f);
                    f = converter.convert(rotate(src, Integer.valueOf(rotate)));
                }
                //输出第几帧图片
                doExecuteFrame(f, targerFilePath, targetFileName + i);
            }
            i++;
        }
        ff.stop();
    }
 
    /*
     * 旋转角度的
     */
    public static IplImage rotate(IplImage src, int angle) {
        IplImage img = IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
        opencv_core.cvTranspose(src, img);
        opencv_core.cvFlip(img, img, angle);
        return img;
    }
 
    public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName) {
 
        if (null == f || null == f.image) {
            return;
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        String imageMat = "jpg";
        String FileName = targerFilePath + File.separator + targetFileName + "." + imageMat;
        BufferedImage bi = converter.getBufferedImage(f);
        System.out.println("width:" + bi.getWidth());
        System.out.println("height:" + bi.getHeight());
        File output = new File(FileName);
        try {
            ImageIO.write(bi, imageMat, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}

标题:JavaCV开发详解之2:截取推流服务器视频的某帧或本地视频的某帧
作者:mmzsblog
地址:https://www.mmzsblog.cn/articles/2020/09/07/1599489221392.html

如未加特殊说明,文章均为原创,转载必须注明出处。均采用CC BY-SA 4.0 协议

本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。若本站转载文章遗漏了原文链接,请及时告知,我们将做删除处理!文章观点不代表本网站立场,如需处理请联系首页客服。
• 网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。
• 公众号转载请联系网站首页的微信号申请白名单!

个人微信公众号 ↓↓↓                 

微信搜一搜 Java 学习之道