org.bytedeco.javacv.FrameGrabber.Exception Java Examples

The following examples show how to use org.bytedeco.javacv.FrameGrabber.Exception. 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: CameraVideoPanel.java    From oim-fx with MIT License 6 votes vote down vote up
public BufferedImage getBufferedImage() {
	Frame capturedFrame = null;
	boolean flipChannels = false;
	BufferedImage bufferedImage = null;
	try {
		if (start) {
			if ((capturedFrame = grabber.grab()) != null) {
				int type = Java2DFrameConverter.getBufferedImageType(capturedFrame);
				double gamma = type == BufferedImage.TYPE_CUSTOM ? 1.0 : inverseGamma;
				bufferedImage = converter.getBufferedImage(capturedFrame, gamma, flipChannels, null);
				Image image = bufferedImage;
				ImageIcon icon = new ImageIcon(image);
				iconLabel.setIcon(icon);
			}
		}

	} catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
		e.printStackTrace();
	}
	return bufferedImage;
}
 
Example #2
Source File: CameraVideoPanel.java    From oim-fx with MIT License 6 votes vote down vote up
public Frame getFrame() {
	Frame capturedFrame = null;
	boolean flipChannels = false;
	BufferedImage bufferedImage = null;
	try {
		if (start) {
			if ((capturedFrame = grabber.grab()) != null) {
				int type = Java2DFrameConverter.getBufferedImageType(capturedFrame);
				double gamma = type == BufferedImage.TYPE_CUSTOM ? 1.0 : inverseGamma;
				bufferedImage = converter.getBufferedImage(capturedFrame, gamma, flipChannels, null);
				Image image = bufferedImage;
				ImageIcon icon = new ImageIcon(image);
				iconLabel.setIcon(icon);
			}
		}

	} catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
		e.printStackTrace();
	}
	return capturedFrame;
}
 
Example #3
Source File: CameraVideo.java    From oim-fx with MIT License 6 votes vote down vote up
public BufferedImage getBufferedImage() {
	Frame capturedFrame = null;
	boolean flipChannels = false;
	BufferedImage bufferedImage = null;
	try {
		if (start) {
			if ((capturedFrame = grabber.grab()) != null) {
				int type = Java2DFrameConverter.getBufferedImageType(capturedFrame);
				double gamma = type == BufferedImage.TYPE_CUSTOM ? 1.0 : inverseGamma;
				bufferedImage = converter.getBufferedImage(capturedFrame, gamma, flipChannels, null);
			}
		}
	} catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
		e.printStackTrace();
	}
	return bufferedImage;
}
 
Example #4
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 #5
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 #6
Source File: CameraVideoPanel.java    From oim-fx with MIT License 5 votes vote down vote up
public void stopVideo() {
	try {
		start = false;
		grabber.stop();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example #7
Source File: FFmpegProcessor.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
private void restart() throws Exception {
	pixmap = null;
	grabber.restart();
	grabber.grabFrame();
	eof = false;
	offset = grabber.getTimestamp() - time * 1000;
	framecount = 1;
	// System.out.println("movie restart - starttime : " + start);
}
 
Example #8
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 #9
Source File: VidImageSequence.java    From GIFKR with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public BufferedImage getFrame(int gifFrame) {
	
	BufferedImage frame = null;
	
	try {
		g.setFrameNumber(gifFrame+1);
		frame = new Java2DFrameConverter().convert(g.grabImage());
	} catch (Exception e) {
		e.printStackTrace();
	}
	return frame == null? new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR) : frame;
}
 
Example #10
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 #11
Source File: VideoThumbnailBuilder.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeVideoFrame(File videoFile, File frameFile) throws Exception {
	FrameGrabber g = new OpenCVFrameGrabber(videoFile);
	g.start();

	/*
	 * Try to get a frame after 60 seconds
	 */
	double frameRate = g.getFrameRate();
	int fiveSecondsFrame = (int) (60 * frameRate);
	if (fiveSecondsFrame > g.getLengthInFrames())
		fiveSecondsFrame = 1;

	try {
		for (int i = 0; i < g.getLengthInFrames() && frameFile.length() == 0; i++) {
			try {
				if (i < fiveSecondsFrame) {
					g.grab();
					continue;
				}

				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 #12
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 #13
Source File: JavaCVReadAVI.java    From Data_Processor with Apache License 2.0 5 votes vote down vote up
public static void main(String[] argv) throws Exception, InterruptedException{
	JavaCVReadAVI t = new JavaCVReadAVI();
	t.setVisible(true);
	t.setPreferredSize(new Dimension(800,300));
	JFrame fr = new JFrame();
	fr.setSize(800, 600);
	fr.add(t);
	fr.setVisible(true);
	fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	t.run();		
}
 
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: JavaCVRecord.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 输出视频到文件或者流服务
 * 
 * @param out
 *            -输出位置(支持流服务和文件)
 * @return
 * @throws IOException
 */
public Recorder to(String out) throws IOException {
	if (out == null) {
		throw new Exception("输出视频不能为空");
	}
	this.out = out;
	// 录制/推流器
	record = new FFmpegFrameRecorderPlus(out, width, height);
	record.setVideoOption("crf", "18");
	record.setGopSize(2);
	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);
	}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 #16
Source File: CameraVideo.java    From oim-fx with MIT License 5 votes vote down vote up
public void stopVideo() {
	try {
		start = false;
		grabber.stop();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #17
Source File: CameraVideo.java    From oim-fx with MIT License 5 votes vote down vote up
public boolean startVideo() {
	try {
		grabber.setImageWidth(captureWidth);
		grabber.setImageHeight(captureHeight);
		grabber.start();
		start = true;
	} catch (Exception e) {
		e.printStackTrace();
		start = false;
		camera = false;
	}
	return start;
}
 
Example #18
Source File: CameraVideoPanel.java    From oim-fx with MIT License 5 votes vote down vote up
public boolean startVideo() {
	try {
		grabber.setImageWidth(captureWidth);
		grabber.setImageHeight(captureHeight);
		grabber.start();
		start = true;
	} catch (Exception e) {
		e.printStackTrace();
		start = false;
	}
	return start;
}
 
Example #19
Source File: ConvertVideoPakcet.java    From easyCV with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception, IOException {
	new ConvertVideoPakcet().from("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov")
	.to("rtmp://eguid.cc:1935/rtmp/eguid")
	.go();
}
 
Example #20
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 #21
Source File: Recorder.java    From easyCV with Apache License 2.0 2 votes vote down vote up
/**
 * 设置视频源
 * 
 * @param src
 *            视频源(文件或流媒体拉流地址)
 * @return
 */
Recorder from(String src) throws Exception;
 
Example #22
Source File: JavaCVRecord.java    From easyCV with Apache License 2.0 2 votes vote down vote up
/**
 * 重新开始,实际链式调用了:from(src).to(out).start()
 * 
 * @return
 * @throws Exception
 * @throws IOException
 */
public Recorder restart() throws Exception, IOException {
	return from(src).to(out).start();
}