org.bytedeco.javacv.FFmpegFrameGrabber Java Examples

The following examples show how to use org.bytedeco.javacv.FFmpegFrameGrabber. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LivePlayTest.java    From oim-fx with MIT License 8 votes vote down vote up
private static void recordByFrame(FFmpegFrameGrabber grabber, FFmpegFrameRecorder recorder, Boolean status)
		throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {
	try {// 建议在线程中使用该方法
		grabber.start();
		recorder.start();
		Frame frame = null;
		while (status && (frame = grabber.grabFrame()) != null) {
			recorder.record(frame);
		}
		recorder.stop();
		grabber.stop();
	} finally {
		if (grabber != null) {
			grabber.stop();
		}
	}
}
 
Example #2
Source File: NativeAudioRecordReader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
protected List<Writable> loadData(File file, InputStream inputStream) throws IOException {
    List<Writable> ret = new ArrayList<>();
    try (FFmpegFrameGrabber grabber = inputStream != null ? new FFmpegFrameGrabber(inputStream)
                    : new FFmpegFrameGrabber(file.getAbsolutePath())) {
        grabber.setSampleFormat(AV_SAMPLE_FMT_FLT);
        grabber.start();
        Frame frame;
        while ((frame = grabber.grab()) != null) {
            while (frame.samples != null && frame.samples[0].hasRemaining()) {
                for (int i = 0; i < frame.samples.length; i++) {
                    ret.add(new FloatWritable(((FloatBuffer) frame.samples[i]).get()));
                }
            }
        }
    }
    return ret;
}
 
Example #3
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 6 votes vote down vote up
private void runVideoMainThread(String videoFileName, OpenCVFrameConverter.ToMat toMat) throws FrameGrabber.Exception {
    FFmpegFrameGrabber grabber = initFrameGrabber(videoFileName);
    while (!stop) {
        Frame frame = grabber.grab();
        if (frame == null) {
            log.info("Stopping");
            stop();
            break;
        }
        if (frame.image == null) {
            continue;
        }
        yolo.push(frame);
        opencv_core.Mat mat = toMat.convert(frame);
        yolo.drawBoundingBoxesRectangles(frame, mat);
        imshow(windowName, mat);
        char key = (char) waitKey(20);
        // Exit this loop on escape:
        if (key == 27) {
            stop();
            break;
        }
    }
}
 
Example #4
Source File: JavaCVReadAVI.java    From Data_Processor with Apache License 2.0 6 votes vote down vote up
public void run() throws Exception, InterruptedException {
		FFmpegFrameGrabber ffmpegFrameGrabber = FFmpegFrameGrabber.createDefault("C:/Users/Administrator/Desktop/deta/detasource/videoProcess/webwxgetvideo.avi");
		ffmpegFrameGrabber.start();
//		int fflength = ffmpegFrameGrabber.getLengthInFrames();
//		int maxStamp = (int) (ffmpegFrameGrabber.getLengthInTime()/1000000);
//		int count = 0;
		while (true) {
			Frame nowFrame = ffmpegFrameGrabber.grabImage();
//			int startStamp = (int) (ffmpegFrameGrabber.getTimestamp() * 1.0/1000000);
//			double present = (startStamp * 1.0 / maxStamp) * 100;
			if (nowFrame == null) {
				System.out.println("!!! Failed cvQueryFrame");
				continue;
			}
			Java2DFrameConverter paintConverter = new Java2DFrameConverter();
			BufferedImage difImage = paintConverter.getBufferedImage(nowFrame, 1);
			paint(difImage);
			Thread.sleep(25);
		}
	}
 
Example #5
Source File: LivePlayTest3.java    From oim-fx with MIT License 6 votes vote down vote up
public void run() {
	Loader.load(opencv_objdetect.class);

	try {
		grabber = FFmpegFrameGrabber.createDefault(url);
		grabber.start();
	} catch (Exception e) {
		try {
			grabber.restart();
		} catch (Exception e1) {
		}
	}
	while (true) {
		// try {
		getBufferedImage();
		// sleep(1);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
	}
}
 
Example #6
Source File: ConverterService.java    From Spring with Apache License 2.0 6 votes vote down vote up
public void toAnimatedGif(FFmpegFrameGrabber frameGrabber, AnimatedGifEncoder
        gifEncoder, int start, int end, int speed) throws FrameGrabber.Exception {

    final long startFrame = Math.round(start * frameGrabber.getFrameRate());
    final long endFrame = Math.round(end * frameGrabber.getFrameRate());

    final Java2DFrameConverter frameConverter = new Java2DFrameConverter();

    for (long i = startFrame; i < endFrame; i++) {

        if (i % speed == 0) {

            // Bug if frameNumber is set to 0
            if (i > 0) {
                frameGrabber.setFrameNumber((int) i);
            }

            final BufferedImage bufferedImage = frameConverter.convert(frameGrabber.grabImage());
            gifEncoder.addFrame(bufferedImage);
        }

    }

    frameGrabber.stop();
    gifEncoder.finish();
}
 
Example #7
Source File: UploadController.java    From Spring with Apache License 2.0 6 votes vote down vote up
/**
 * curl -F file=@/home/olinnyk/IdeaProjects/Spring/SpringWEB/SpringBoot/just-gif-it/video/PexelsVideos.mp4 -F start=0 -F end=5 -F speed=1 -F repeat=0 localhost:8080/upload
 */
@PostMapping
@RequestMapping(value = "/upload", produces = MediaType.IMAGE_GIF_VALUE)
public String upload(@RequestPart("file") MultipartFile file,
		@RequestParam("start") int start,
		@RequestParam("end") int end,
		@RequestParam("speed") int speed,
		@RequestParam("repeat") boolean repeat) throws IOException, FrameGrabber.Exception {

	final File videoFile = new File(location + "/" + System.currentTimeMillis() + ".mp4");
	file.transferTo(videoFile);

	log.info("Saved video file to {}", videoFile.getAbsolutePath());

	final Path output = Paths.get(location + "/gif/" + System.currentTimeMillis() + ".gif");

	final FFmpegFrameGrabber frameGrabber = videoDecoderService.read(videoFile);
	final AnimatedGifEncoder gifEncoder = gifEncoderService.getGifEncoder(repeat, (float) frameGrabber.getFrameRate(), output);
	converterService.toAnimatedGif(frameGrabber, gifEncoder, start, end, speed);

	log.info("Saved generated gif to {}", output.toString());

	return output.getFileName().toString();
}
 
Example #8
Source File: NativeAudioRecordReader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
protected List<Writable> loadData(File file, InputStream inputStream) throws IOException {
    List<Writable> ret = new ArrayList<>();
    try (FFmpegFrameGrabber grabber = inputStream != null ? new FFmpegFrameGrabber(inputStream)
                    : new FFmpegFrameGrabber(file.getAbsolutePath())) {
        grabber.setSampleFormat(AV_SAMPLE_FMT_FLT);
        grabber.start();
        Frame frame;
        while ((frame = grabber.grab()) != null) {
            while (frame.samples != null && frame.samples[0].hasRemaining()) {
                for (int i = 0; i < frame.samples.length; i++) {
                    ret.add(new FloatWritable(((FloatBuffer) frame.samples[i]).get()));
                }
            }
        }
    }
    return ret;
}
 
Example #9
Source File: CameraFFMPEG.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
     * TODO: Check the use of this. 
     * @deprecated
     */
    @Deprecated
    public void startVideo() {
        FFmpegFrameGrabber grabberFF = new FFmpegFrameGrabber(this.cameraDescription);
        try {
            grabberFF.setFrameRate(30);
            this.setPixelFormat(PixelFormat.BGR);
            grabberFF.start();
            this.grabber = grabberFF;
            this.setSize(grabber.getImageWidth(), grabber.getImageHeight());
//            this.setFrameRate((int) grabberFF.getFrameRate());
            grabberFF.setFrameRate(30);
            this.isConnected = true;
        } catch (Exception e) {
            System.err.println("Could not FFMPEG frameGrabber... " + e);
            System.err.println("Camera ID " + this.cameraDescription + ":" + this.imageFormat + " could not start.");
            System.err.println("Check cable connection, ID and resolution asked.");
            this.grabber = null;
        }
    }
 
Example #10
Source File: CameraFFMPEG.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void start() {
    FFmpegFrameGrabber grabberFF = new FFmpegFrameGrabber(this.cameraDescription);

    grabberFF.setImageMode(FrameGrabber.ImageMode.COLOR);

    this.setPixelFormat(PixelFormat.BGR);

    grabberFF.setFormat(this.imageFormat);
    grabberFF.setImageWidth(width());
    grabberFF.setImageHeight(height());
    grabberFF.setFrameRate(frameRate);

    try {
        grabberFF.start();
        this.grabber = grabberFF;
        this.isConnected = true;
    } catch (FrameGrabber.Exception e) {
        System.err.println("Could not FFMPEG frameGrabber... " + e);
        System.err.println("Camera ID " + this.cameraDescription + ":" + this.imageFormat + " could not start.");
        System.err.println("Check cable connection, ID and resolution asked.");
        this.grabber = null;
    }
}
 
Example #11
Source File: JavaCVRecord.java    From easyCV with Apache License 2.0 6 votes vote down vote up
/**
 * 视频源
 * 
 * @param src
 * @return
 * @throws Exception
 */
public Recorder from(String src) throws Exception {
	av_log_set_level(AV_LOG_ERROR);
	if (src == null) {
		throw new Exception("源视频不能为空");
	}
	this.src = src;
	// 采集/抓取器
	grabber = new FFmpegFrameGrabber(src);
	if (hasRTSP(src)) {
		grabber.setOption("rtsp_transport", "tcp");
	}
	grabber.start();// 开始之后ffmpeg会采集视频信息,之后就可以获取音视频信息
	if (width < 0 || height < 0) {
		width = grabber.getImageWidth();
		height = grabber.getImageHeight();
	}
	// 视频参数
	audiocodecid = grabber.getAudioCodec();
	codecid = grabber.getVideoCodec();
	framerate = grabber.getVideoFrameRate();// 帧率
	bitrate = grabber.getVideoBitrate();// 比特率
	// 音频参数
	// 想要录制音频,这三个参数必须有:audioChannels > 0 && audioBitrate > 0 && sampleRate > 0
	audioChannels = grabber.getAudioChannels();
	audioBitrate = grabber.getAudioBitrate();
	if (audioBitrate < 1) {
		audioBitrate = 128 * 1000;// 默认音频比特率
	}
	sampleRate = grabber.getSampleRate();
	return this;
}
 
Example #12
Source File: RecordThread.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param name -线程名称
 * @param grabber -抓取器
 * @param record -录制器
 * @param err_stop_num 允许的错误次数,超过该次数后即停止任务
 */
public RecordThread(String name,FFmpegFrameGrabber grabber, FFmpegFrameRecorderPlus record,Integer err_stop_num) {
	super(name);
	this.grabber = grabber;
	this.record = record;
	if(err_stop_num!=null) {
		this.err_stop_num=err_stop_num;
	}
}
 
Example #13
Source File: InstagramUploadResumableVideoRequest.java    From instagram4j with Apache License 2.0 5 votes vote down vote up
public String[] getVideoInfo() {
	try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoFile)) {
		frameGrabber.start();

		return new String[] { String.valueOf(frameGrabber.getLengthInTime() / 1000l),
				String.valueOf(frameGrabber.getImageHeight()), String.valueOf(frameGrabber.getImageWidth()) };
	} catch (Exception e) {
		log.error("Exception occured when trying to grab video information: " + e.getMessage());
		return new String[] { "0", "0", "0" };
	}
}
 
Example #14
Source File: ConvertVideoPakcet.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 选择视频源
 * @param src
 * @author eguid
 * @throws Exception
 */
public ConvertVideoPakcet from(String src) throws Exception {
	// 采集/抓取器
	grabber = new FFmpegFrameGrabber(src);
	if(src.indexOf("rtsp")>=0) {
		grabber.setOption("rtsp_transport","tcp");
	}
	grabber.start();// 开始之后ffmpeg会采集视频信息,之后就可以获取音视频信息
	if (width < 0 || height < 0) {
		width = grabber.getImageWidth();
		height = grabber.getImageHeight();
	}
	// 视频参数
	audiocodecid = grabber.getAudioCodec();
	System.err.println("音频编码:" + audiocodecid);
	codecid = grabber.getVideoCodec();
	framerate = grabber.getVideoFrameRate();// 帧率
	bitrate = grabber.getVideoBitrate();// 比特率
	// 音频参数
	// 想要录制音频,这三个参数必须有:audioChannels > 0 && audioBitrate > 0 && sampleRate > 0
	audioChannels = grabber.getAudioChannels();
	audioBitrate = grabber.getAudioBitrate();
	if (audioBitrate < 1) {
		audioBitrate = 128 * 1000;// 默认音频比特率
	}
	return this;
}
 
Example #15
Source File: LivePlayTest.java    From oim-fx with MIT License 5 votes vote down vote up
/**
 * 按帧录制视频
 * 
 * @param inputFile-该地址可以是网络直播/录播地址,也可以是远程/本地文件路径
 * @param outputFile
 *            -该地址只能是文件地址,如果使用该方法推送流媒体服务器会报错,原因是没有设置编码格式
 * @throws FrameGrabber.Exception
 * @throws FrameRecorder.Exception
 * @throws org.bytedeco.javacv.FrameRecorder.Exception
 */
public static void frameRecord(String inputFile, String outputFile, int audioChannel)
		throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {

	boolean isStart = true;// 该变量建议设置为全局控制变量,用于控制录制结束
	// 获取视频源
	FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);
	// 流媒体输出地址,分辨率(长,高),是否录制音频(0:不录制/1:录制)
	FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, 1280, 720, audioChannel);
	// 开始取视频源
	recordByFrame(grabber, recorder, isStart);
}
 
Example #16
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void runVideoMainThread(Yolo yolo, String windowName,
                                String videoFileName,
                                OpenCVFrameConverter.ToMat toMat) throws Exception {
    FFmpegFrameGrabber grabber = initFrameGrabber(videoFileName);
    while (!stop) {
        Frame frame = grabber.grab();

        if (frame == null) {
            log.info("Stopping");
            stop();
            break;
        }
        if (frame.image == null) {
            continue;
        }

        Thread.sleep(60);
        opencv_core.Mat mat = toMat.convert(frame);
        opencv_core.Mat resizeMat = new opencv_core.Mat(yolo.getSelectedSpeed().height,
                yolo.getSelectedSpeed().width, mat.type());
        yolo.push(resizeMat, windowName);
        org.bytedeco.javacpp.opencv_imgproc.resize(mat, resizeMat, resizeMat.size());
        yolo.drawBoundingBoxesRectangles(frame, resizeMat, windowName);
        char key = (char) waitKey(20);
        // Exit this loop on escape:
        if (key == 27) {
            stop();
            break;
        }
    }
}
 
Example #17
Source File: VidImageSequence.java    From GIFKR with GNU Lesser General Public License v3.0 5 votes vote down vote up
public VidImageSequence(File f) throws IOException {
	
	super(f.getName());
	
	try {
		g = new FFmpegFrameGrabber(f);
		g.start();
		width	= g.getImageWidth();
		height	= g.getImageHeight();
		frames	= g.getLengthInFrames();
		
	} catch (Exception e) {
		throw new IOException();
	}
}
 
Example #18
Source File: VideoThumbnailBuilder.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeMp4Frame(File videoFile, File frameFile) throws Exception {
	FFmpegFrameGrabber g = new FFmpegFrameGrabber(videoFile);
	g.start();

	/*
	 * Get a frame in the middle of the video
	 */
	int startFrame = g.getLengthInVideoFrames() / 2;
	g.setVideoFrameNumber(startFrame);

	try {
		for (int i = startFrame; i < g.getLengthInFrames() && frameFile.length() == 0; i++) {
			try {
				Frame frame = g.grab();
				if (frame == null)
					continue;
				BufferedImage img = Java2DFrameUtils.toBufferedImage(frame);
				if (img == null)
					continue;

				ImageIO.write(img, "png", frameFile);
			} catch (Throwable t) {
			}
		}
	} finally {
		g.stop();
		g.close();
	}
}
 
Example #19
Source File: VideoDecoderService.java    From Spring with Apache License 2.0 4 votes vote down vote up
public FFmpegFrameGrabber read(File video) throws FrameGrabber.Exception {
    FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(video);
    frameGrabber.start();
    return frameGrabber;
}
 
Example #20
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 4 votes vote down vote up
private FFmpegFrameGrabber initFrameGrabber(String videoFileName) throws FrameGrabber.Exception {
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFileName);
    grabber.start();
    return grabber;
}
 
Example #21
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 4 votes vote down vote up
private FFmpegFrameGrabber initFrameGrabber(String videoFileName) throws FrameGrabber.Exception {
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(new File(videoFileName));
    grabber.start();
    return grabber;
}
 
Example #22
Source File: RecordThread.java    From easyCV with Apache License 2.0 4 votes vote down vote up
public void setGrabber(FFmpegFrameGrabber grabber) {
	this.grabber = grabber;
}
 
Example #23
Source File: RecordThread.java    From easyCV with Apache License 2.0 4 votes vote down vote up
public FFmpegFrameGrabber getGrabber() {
	return grabber;
}
 
Example #24
Source File: RecordThread.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
 * 运行过一次后必须进行重置参数和运行状态
 */
public void reset(FFmpegFrameGrabber grabber, FFmpegFrameRecorderPlus record) {
	this.grabber = grabber;
	this.record = record;
	
}
 
Example #25
Source File: JavaCVRecord.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
 * 转发源视频到输出(复制)
 * 
 * @param src
 *            -源视频
 * @param out
 *            -输出流媒体服务地址
 * @return
 * @throws org.bytedeco.javacv.FrameRecorder.Exception
 */
public Recorder stream(String src, String out) throws IOException {
	if (src == null || out == null) {
		throw new Exception("源视频和输出为空");
	}
	this.src = src;
	this.out = out;
	// 采集/抓取器
	grabber = new FFmpegFrameGrabber(src);
	grabber.start();
	if (width < 0 || height < 0) {
		width = grabber.getImageWidth();
		height = grabber.getImageHeight();
	}
	// 视频参数
	int audiocodecid = grabber.getAudioCodec();
	int codecid = grabber.getVideoCodec();
	double framerate = grabber.getVideoFrameRate();// 帧率
	int bitrate = grabber.getVideoBitrate();// 比特率

	// 音频参数
	// 想要录制音频,这三个参数必须有:audioChannels > 0 && audioBitrate > 0 && sampleRate > 0
	int audioChannels = grabber.getAudioChannels();
	int audioBitrate = grabber.getAudioBitrate();
	int sampleRate = grabber.getSampleRate();

	// 录制/推流器
	record = new FFmpegFrameRecorderPlus(out, width, height);
	record.setVideoOption("crf", "18");
	record.setGopSize(1);

	record.setFrameRate(framerate);
	record.setVideoBitrate(bitrate);
	record.setAudioChannels(audioChannels);
	record.setAudioBitrate(audioBitrate);
	record.setSampleRate(sampleRate);
	AVFormatContext fc = null;
	//rtmp和flv
	if (hasRTMPFLV(out)) {
		// 封装格式flv,并使用h264和aac编码
		record.setFormat("flv");
		record.setVideoCodec(AV_CODEC_ID_H264);
		record.setAudioCodec(AV_CODEC_ID_AAC);
		if(hasRTMPFLV(src)) {
			fc = grabber.getFormatContext();
		}
	}else if(hasMP4(out)){//MP4
		record.setFormat("mp4");
		record.setVideoCodec(AV_CODEC_ID_H264);
		record.setAudioCodec(AV_CODEC_ID_AAC);
	}
	record.start(fc);
	return this;
}
 
Example #26
Source File: LivePlayTest2.java    From oim-fx with MIT License 4 votes vote down vote up
/**
 * 转流器
 * 
 * @param inputFile
 * @param outputFile
 * @throws Exception
 * @throws org.bytedeco.javacv.FrameRecorder.Exception
 * @throws InterruptedException
 */
public static void recordPush(String inputFile, int v_rs) throws Exception, org.bytedeco.javacv.FrameRecorder.Exception, InterruptedException {
	Loader.load(opencv_objdetect.class);
	long startTime = 0;
	FrameGrabber grabber = FFmpegFrameGrabber.createDefault(inputFile);
	try {
		grabber.start();
	} catch (Exception e) {
		try {
			grabber.restart();
		} catch (Exception e1) {
			throw e;
		}
	}

	OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
	Frame grabframe = grabber.grab();
	IplImage grabbedImage = null;
	if (grabframe != null) {
		System.out.println("取到第一帧");
		grabbedImage = converter.convert(grabframe);
	} else {
		System.out.println("没有取到第一帧");
	}

	System.out.println("开始推流");
	CanvasFrame frame = new CanvasFrame("camera", CanvasFrame.getDefaultGamma() / grabber.getGamma());
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setAlwaysOnTop(true);
	while (frame.isVisible() && (grabframe = grabber.grab()) != null) {
		System.out.println("推流...");
		frame.showImage(grabframe);
		grabbedImage = converter.convert(grabframe);
		Frame rotatedFrame = converter.convert(grabbedImage);

		if (startTime == 0) {
			startTime = System.currentTimeMillis();
		}

		Thread.sleep(40);
	}
	frame.dispose();

	grabber.stop();
	System.exit(2);
}