Created
June 20, 2015 06:46
-
-
Save guffyWave/fd9b26548578258b4dc7 to your computer and use it in GitHub Desktop.
Logic20June1215PM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.appxperts.opencvfirstapp; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import org.bytedeco.javacpp.Pointer; | |
import org.bytedeco.javacpp.avcodec; | |
import org.bytedeco.javacpp.opencv_core; | |
import org.bytedeco.javacpp.opencv_imgproc; | |
import org.bytedeco.javacv.FFmpegFrameGrabber; | |
import org.bytedeco.javacv.FFmpegFrameRecorder; | |
import org.bytedeco.javacv.Frame; | |
import org.bytedeco.javacv.FrameGrabber; | |
import org.bytedeco.javacv.FrameRecorder; | |
import org.bytedeco.javacv.OpenCVFrameConverter; | |
import java.io.File; | |
/** | |
* Created by PC on 6/18/2015. | |
*/ | |
public class MainActivitySecond extends Activity { | |
ImageView image_view; | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
private static final String IMAGE_PATH = Environment.getExternalStorageDirectory() + File.separator + "RanjanJavaCv" + File.separator; | |
// private static final String IMAGE_PATH = Environment.getExternalStorageDirectory() + File.separator + "Myvideo_result.mp4"; | |
// private static final String VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; | |
//private static final String VIDEO_URL = "http://r5---sn-cvh7zn7k.c.youtube.com/videoplayback?id=32541e2e84d4a191&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=903309%2C906398%2C935031%2C916611%2C924616%2C924610%2C907231&sver=3&cp=U0hXR1ZNTl9FSkNON19OR1JKOmtlYmdzNVE4ZG5N&upn=VopUQRFCawg&ms=au&mt=1382596803&mv=u&ip=59.162.119.108&key=yt5&itag=18&expire=1382620900&ratebypass=yes&ipbits=8&source=youtube&signature=E77065B4FAC80972F255857BB16856C601F77734.BBD905340F324DFD55BCF39D31617EE9A7FB1B37"; | |
private static final File m_file = new File(Environment.getExternalStorageDirectory() + File.separator, "VideoEffects" + File.separator + "vid.mp4"); | |
// private static final File m_file = new File(Environment.getExternalStorageDirectory() + File.separator, "Myvideo.mp4"); | |
final long MICROSECOND = 1000000; | |
private File m_imageDirectory = null; | |
private Button m_btnGrab = null; | |
//private int m_count = 1; | |
private volatile int m_frameCount = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
image_view = (ImageView) findViewById(R.id.image_view); | |
m_btnGrab = (Button) findViewById(R.id.button); | |
m_imageDirectory = new File(IMAGE_PATH); | |
if (!m_imageDirectory.exists()) { | |
m_imageDirectory.mkdirs(); | |
} | |
m_btnGrab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
kaamdg(); | |
// new GrabberThread(1, 5).start(); | |
//new GrabberThread(6, 10).start(); | |
//new GrabberThread(11, 15).start(); | |
} | |
}); | |
} | |
public void kaamdg() { | |
FFmpegFrameGrabber ffg = new FFmpegFrameGrabber(m_file.getAbsolutePath()); | |
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage(); | |
FrameRecorder recorder = null; | |
try { | |
ffg.start(); | |
recorder = new FFmpegFrameRecorder(Environment.getExternalStorageDirectory() + File.separator | |
+ "VideoEffects" + File.separator + "result_test.mp4", ffg.getImageWidth(), ffg.getImageHeight(), | |
ffg.getAudioChannels()); | |
recorder.setFrameRate(ffg.getFrameRate()); | |
recorder.setSampleRate(ffg.getSampleRate()); | |
recorder.setSampleFormat(ffg.getSampleFormat()); | |
// recorder.setAudioCodec(AV_CODEC_ID_AAC); | |
// recorder.setVideoCodec(AV_CODEC_ID_H264); | |
//recorder.setVideoCodec(ffg.getVideoCodec()); | |
recorder.setVideoOption("tune", "zerolatency"); | |
// tradeoff between quality and encode speed | |
// possible values are ultrafast,superfast, veryfast, faster, fast, | |
// medium, slow, slower, veryslow | |
// ultrafast offers us the least amount of compression (lower encoder | |
// CPU) at the cost of a larger stream size | |
// at the other end, veryslow provides the best compression (high | |
// encoder CPU) while lowering the stream size | |
// (see: https://trac.ffmpeg.org/wiki/Encode/H.264) | |
recorder.setVideoOption("preset", "ultrafast"); | |
// Constant Rate Factor (see: https://trac.ffmpeg.org/wiki/Encode/H.264) | |
recorder.setVideoOption("crf", "28"); | |
// 2000 kb/s, reasonable "sane" area for 720 | |
recorder.setVideoBitrate(ffg.getVideoBitrate()); | |
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); | |
recorder.setFormat("flv"); | |
// FPS (frames per second) | |
// Key frame interval, in our case every 2 seconds -> 30 (fps) * 2 = 60 | |
// (gop length) | |
recorder.setGopSize(60); | |
// We don't want variable bitrate audio | |
recorder.setAudioOption("crf", "0"); | |
// Highest quality | |
recorder.setAudioQuality(0); | |
// 192 Kbps | |
recorder.setAudioBitrate(ffg.getAudioBitrate()); | |
recorder.setAudioChannels(ffg.getAudioChannels()); | |
recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); | |
// recorder.setFormat("flv"); | |
try { | |
recorder.start(); | |
} catch (FrameRecorder.Exception e) { | |
e.printStackTrace(); | |
} | |
int count = 0; | |
while (true) { | |
Log.d("JAVA_CV", "Counting . . . " + count); | |
Frame original_frame = ffg.grabFrame(); | |
if (original_frame == null) { | |
Log.d("JAVA_CV", "original_frame is NULL at" + count); | |
break; | |
} else { | |
Log.d("JAVA_CV", "original_frame is NOT_NULL at" + count); | |
// | |
opencv_core.IplImage iplImage = null; | |
if (original_frame != null) { | |
try { | |
if (original_frame.image != null) { | |
if (original_frame.image.length > 0) { | |
Pointer p = new Pointer(original_frame.image[0]); | |
Long l=Long.valueOf(p.address()); | |
if (l != null) { | |
Log.d("JAVA_CV", "\t\t --------------->>>> "); | |
// Log.d("JAVA_CV", "\t\t original_frame at " + count + " has image buffer size: " + original_frame.image.length); | |
Log.d("JAVA_CV", "\t\t with height " + original_frame.imageHeight); | |
Log.d("JAVA_CV", "\t\t with width " + original_frame.imageWidth); | |
Log.d("JAVA_CV", "\t\t with depth " + original_frame.imageDepth); | |
Log.d("JAVA_CV", "\t\t ---------------------------------------------------------->>>> "); | |
iplImage = converter.convert(original_frame); | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
// continue; | |
} | |
} | |
if (iplImage == null) { | |
// Log.d("JAVA_CV", "iplImage is NULL at" + count); | |
} else { | |
if (!iplImage.isNull() && original_frame.image != null) { | |
int width = iplImage.width(); | |
int height = iplImage.height(); | |
int channel = iplImage.nChannels(); | |
opencv_core.IplImage iplImage2 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, channel); | |
if (iplImage2 == null) | |
continue; | |
opencv_core.cvCopy(iplImage, iplImage2); | |
opencv_core.IplImage iplImage3 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, 1); | |
if (iplImage3 == null) | |
continue; | |
opencv_imgproc.cvCvtColor(iplImage2, iplImage3, opencv_imgproc.CV_BGRA2GRAY); | |
/*Bitmap m_bitmap = Bitmap.createBitmap(frame3.width(), frame3.height(), Bitmap.Config.ALPHA_8); | |
m_bitmap.copyPixelsFromBuffer(iplImage2.getByteBuffer()); | |
image_view.setImageBitmap(m_bitmap);*/ | |
// Log.d("JAVA_CV", "iplImage is NOT_NULL at" + count + " Height " + iplImage.height()); | |
Frame resultFrame = converter.convert(iplImage3); | |
if (resultFrame == null) | |
continue; | |
recorder.record(resultFrame); | |
iplImage.release(); | |
iplImage2.release(); | |
iplImage3.release(); | |
original_frame = null; | |
} else { | |
break; | |
} | |
} | |
//opencv_core.IplImage frame2 = opencv_core.IplImage.create(original_frame.imageWidth, original_frame.imageHeight, opencv_core.IPL_DEPTH_8U, 0); | |
// opencv_imgproc.cvCvtColor(iplImage, frame2, opencv_imgproc.CV_RGB2GRAY); | |
// opencv_imgproc. | |
// if (count == 0) { | |
// Bitmap m_bitmap = Bitmap.createBitmap(frame2.width(), frame2.height(), Bitmap.Config.ARGB_8888); | |
// m_bitmap.copyPixelsFromBuffer(frame2.getByteBuffer()); | |
// image_view.setImageBitmap(m_bitmap); | |
// break; | |
// } | |
} | |
count++; | |
} | |
} catch (FrameGrabber.Exception fe) { | |
fe.printStackTrace(); | |
System.out.println(fe.getMessage()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println(e.getMessage()); | |
} finally { | |
try { | |
ffg.release(); | |
try { | |
recorder.release(); | |
} catch (FrameRecorder.Exception e) { | |
e.printStackTrace(); | |
} | |
} catch (FrameGrabber.Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
/* public void kaamdg() { | |
; | |
FFmpegFrameGrabber ffg = new FFmpegFrameGrabber(m_file.getAbsolutePath()); | |
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage(); | |
FrameRecorder recorder = new FFmpegFrameRecorder(Environment.getExternalStorageDirectory() + File.separator | |
+ "VideoEffects" + File.separator + "result_test.mp4", ffg.getImageWidth(), ffg.getImageHeight(), | |
ffg.getAudioChannels()); | |
recorder.setFrameRate(ffg.getFrameRate()); | |
recorder.setSampleRate(ffg.getSampleRate()); | |
recorder.setFormat("mp4"); | |
try { | |
ffg.start(); | |
// recorder.start(); | |
int count = 0; | |
while (true) { | |
Log.d("JAVA_CV", "Counting . . . " + count); | |
Frame original_image_frame = ffg.grab(); | |
Frame original_audio_frame = ffg.grabFrame(); | |
if (original_image_frame == null) { | |
Log.d("JAVA_CV", "original_image_frame is NULL at" + count); | |
break; | |
} else { | |
Log.d("JAVA_CV", "original_image_frame is NOT_NULL at" + count); | |
opencv_core.IplImage frame1 = converter.convert(original_image_frame); | |
if (frame1 == null) { | |
Log.d("JAVA_CV", "frame1 is NULL at" + count); | |
} else { | |
int width = frame1.width(); | |
int height = frame1.height(); | |
int channel = frame1.nChannels(); | |
opencv_core.IplImage frame2 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, channel); | |
opencv_core.cvCopy(frame1, frame2); | |
opencv_core.IplImage frame3 = opencv_core.IplImage.create(width, height, opencv_core.IPL_DEPTH_8U, 1); | |
opencv_imgproc.cvCvtColor(frame2, frame3, opencv_imgproc.CV_BGRA2GRAY); | |
// Bitmap m_bitmap = Bitmap.createBitmap(frame3.width(), frame3.height(), Bitmap.Config.ALPHA_8); | |
// m_bitmap.copyPixelsFromBuffer(frame2.getByteBuffer()); | |
// image_view.setImageBitmap(m_bitmap); | |
// Log.d("JAVA_CV", "frame1 is NOT_NULL at" + count + " Height " + frame1.height()); | |
*//* Frame resultFrame = converter.convert(frame3); | |
*//**//*if (resultFrame == null) { | |
break; | |
} else { | |
recorder.record(resultFrame); | |
if (original_audio_frame.image == null) { | |
recorder.record(original_audio_frame); | |
} | |
}*//* | |
frame1.release(); | |
frame2.release(); | |
frame3.release(); | |
} | |
} | |
count++; | |
} | |
Toast.makeText(getApplicationContext(), "Work DOne ", Toast.LENGTH_LONG).show(); | |
} catch (FrameGrabber.Exception fe) { | |
fe.printStackTrace(); | |
System.out.println(fe.getMessage()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println(e.getMessage()); | |
} finally { | |
try { | |
recorder.stop(); | |
ffg.stop(); | |
recorder.release(); | |
ffg.release(); | |
} catch (FrameRecorder.Exception e) { | |
e.printStackTrace(); | |
} catch (FrameGrabber.Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment