org.bytedeco.javacv.FFmpegFrameRecorder Java Examples

The following examples show how to use org.bytedeco.javacv.FFmpegFrameRecorder. 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: Recorder.java    From VideoAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    if (mFrame != null && mRecording) {
        ((ByteBuffer)mFrame.image[0].position(0)).put(data);

        try {
            long t = 1000 * (System.currentTimeMillis() - mStartTime);
            if (t > mFFmpegFrameRecorder.getTimestamp()) {
                mFFmpegFrameRecorder.setTimestamp(t);
            }

            mFFmpegFrameFilter.push(mFrame);
            Frame frame2;
            while ((frame2 = mFFmpegFrameFilter.pull()) != null) {
                mFFmpegFrameRecorder.record(frame2);            //录制该图片
            }
        } catch (FFmpegFrameRecorder.Exception | FrameFilter.Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example #3
Source File: RecordActivity.java    From PedestrianDetectionSystem with Apache License 2.0 6 votes vote down vote up
@Override
        public void onPreviewFrame(byte[] data, Camera camera) {
//            if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
//                startTime = System.currentTimeMillis();
//                return;
//            }
            if (RECORD_LENGTH > 0) {
                int i = imagesIndex++ % images.length;
                yuvImage = images[i];
                timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
            }
            /* get video data */
            if (yuvImage != null && recording && previewTime ++ % 6 == 0) {
                ((ByteBuffer) yuvImage.image[0].position(0)).put(data);

                if (RECORD_LENGTH <= 0) try {
                    Log.v(LOG_TAG, "Writing Frame");
                    long t = 1000 * (System.currentTimeMillis() - startTime);
                    if (t > recorder.getTimestamp()) {
                        recorder.setTimestamp(t);
                    }
                    recorder.record(yuvImage);
                } catch (FFmpegFrameRecorder.Exception e) {
                    Log.v(LOG_TAG, e.getMessage());
                    e.printStackTrace();
                }
            }
        }
 
Example #4
Source File: VideoRecorder.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Initiate the recording of a video
 * @param filename Name of the video file to create
 * @throws Exception
 */
public void startRecording(String filename) throws Exception {
    stopRecording();

    fmpegFrameRecorder = new FFmpegFrameRecorder(filename, width, height);
    fmpegFrameRecorder.setVideoCodec(codec);
    fmpegFrameRecorder.setFrameRate(framerate);
    fmpegFrameRecorder.setVideoQuality(videoQuality);
    fmpegFrameRecorder.start();
}
 
Example #5
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 #6
Source File: FFmpegFrameRecorderPlus.java    From easyCV with Apache License 2.0 5 votes vote down vote up
public static void tryLoad() throws Exception {
    if (loadingException != null) {
        throw loadingException;
    } else {
        try {
            Loader.load(org.bytedeco.javacpp.avutil.class);
            Loader.load(org.bytedeco.javacpp.swresample.class);
            Loader.load(org.bytedeco.javacpp.avcodec.class);
            Loader.load(org.bytedeco.javacpp.avformat.class);
            Loader.load(org.bytedeco.javacpp.swscale.class);

            /* initialize libavcodec, and register all codecs and formats */
            av_jni_set_java_vm(Loader.getJavaVM(), null);
            avcodec_register_all();
            av_register_all();
            avformat_network_init();

            Loader.load(org.bytedeco.javacpp.avdevice.class);
            avdevice_register_all();
        } catch (Throwable t) {
            if (t instanceof Exception) {
                throw loadingException = (Exception)t;
            } else {
                throw loadingException = new Exception("Failed to load " + FFmpegFrameRecorder.class, t);
            }
        }
    }
}
 
Example #7
Source File: Recorder.java    From VideoAndroid with Apache License 2.0 5 votes vote down vote up
private void initRecorder() {
    Camera.Size previewSize = CameraHelper.getFullScreenPreviewSize(mBuilder.mContext, mBuilder.mCamera);
    mFrame = new Frame(previewSize.width, previewSize.height, Frame.DEPTH_UBYTE, 2);

    //提前加载2个framerecorder
    mFFmpegFrameRecorderV = new FFmpegFrameRecorder(mBuilder.mOutputFilePath, mBuilder.mOutputWidth, mBuilder.mOutputHeight, 1);
    initFrameRecorder(mFFmpegFrameRecorderV);
    mFFmpegFrameRecorderL = new FFmpegFrameRecorder(mBuilder.mOutputFilePath, mBuilder.mOutputHeight, mBuilder.mOutputWidth, 1);
    initFrameRecorder(mFFmpegFrameRecorderL);

    //音频
    mAudioRecordRunnable = new AudioRecordRunnable();
}
 
Example #8
Source File: Recorder.java    From VideoAndroid with Apache License 2.0 5 votes vote down vote up
private void initFrameRecorder(FFmpegFrameRecorder frameRecorder) {
    frameRecorder.setFormat("mp4");
    frameRecorder.setVideoOption("preset", "ultrafast");        //加速
    frameRecorder.setFrameRate(frameRate);
    frameRecorder.setVideoQuality(0);

    frameRecorder.setAudioChannels(1);
    frameRecorder.setSampleRate(sampleAudioRateInHz);
}
 
Example #9
Source File: Recorder.java    From VideoAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

    // Audio
    int bufferSize;
    ShortBuffer audioData;
    int bufferReadResult;

    bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,
            AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioRateInHz,
            AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

    audioData = ShortBuffer.allocate(bufferSize);

    mAudioRecord.startRecording();

    /* ffmpeg_audio encoding loop */
    while (mRunAudioThread) {
        //获取音频数据
        bufferReadResult = mAudioRecord.read(audioData.array(), 0, audioData.capacity());
        audioData.limit(bufferReadResult);
        if (bufferReadResult > 0) {
            if(mFFmpegFrameRecorder != null && mRecording) {
                try {
                    mFFmpegFrameRecorder.recordSamples(audioData);      //写入音频数据
                } catch (FFmpegFrameRecorder.Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /* encoding finish, release recorder */
    if (mAudioRecord != null) {
        mAudioRecord.stop();
        mAudioRecord.release();
    }
}
 
Example #10
Source File: RecordActivity.java    From PedestrianDetectionSystem with Apache License 2.0 5 votes vote down vote up
public void startRecording() {

        initRecorder();

        try {
            recorder.start();
            startTime = System.currentTimeMillis();
            recording = true;
//            audioThread.start();

        } catch (FFmpegFrameRecorder.Exception e) {
            e.printStackTrace();
        }
    }
 
Example #11
Source File: Animation.java    From GIFKR with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void saveVid(File f, int width, int fps, ProgressDisplay d, ActionListener onFinish) {
	saveStopped = false;
	new Thread(() -> {
		try {
			d.setProgress(0, "Starting export");
			d.setCancel(ae -> saveStopped = true);

			String name	= f.getName();
			int dotIdx	= name.lastIndexOf('.');
			if(dotIdx !=- 1)
				name = name.substring(0, dotIdx);
			String ext = ".mp4";

			File out = StringUtil.resolveConflictName(f.getParentFile(), name+ext, false);

			int height = (int)Math.round(width * getSourceHeight()/(float)getSourceWidth());
			height = height %2 == 0 ? height : height + 1; 
			FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(out.getAbsolutePath(), width, height);
			recorder.setFrameRate(fps);
			recorder.setFormat("mp4");
			recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
			//recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
			recorder.setVideoBitrate(recorder.getImageWidth() * recorder.getImageHeight() * fps * 10);

			recorder.setVideoQuality(.1);
			recorder.start();

			for(int i = 0; i < frames && !saveStopped; i++) {
				try {
					setX(i/(float)(frames-1));
					d.setProgress((i/(float)(frames-1))*.95f, "Writing frame "+i+" of " + frames);
					BufferedImage frame = renderFrame(width);
					BufferedImage img = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB);
					img.getGraphics().drawImage(frame, 0, 0, null);
					recorder.record(new Java2DFrameConverter().convert(img), avutil.AV_PIX_FMT_ARGB);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			d.setProgress(.98, "Finishing export...");
			recorder.stop();
			recorder.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			if(onFinish != null)
				SwingUtilities.invokeLater(() -> onFinish.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_FIRST, "save")));
		}
	}).start();
}
 
Example #12
Source File: FFmpegFrameRecorderPlus.java    From easyCV with Apache License 2.0 votes vote down vote up
public static FFmpegFrameRecorder createDefault(File f, int w, int h)   throws Exception { return new FFmpegFrameRecorder(f, w, h); } 
Example #13
Source File: FFmpegFrameRecorderPlus.java    From easyCV with Apache License 2.0 votes vote down vote up
public static FFmpegFrameRecorder createDefault(String f, int w, int h) throws Exception { return new FFmpegFrameRecorder(f, w, h); }