PageRenderTime 330ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BabySelfie/app/src/main/java/babyselfie/mobi/bot/MovieCreator.java

https://gitlab.com/victorbotsales/babySelfie
Java | 119 lines | 83 code | 21 blank | 15 comment | 3 complexity | 798c02ece4cdc19745ab06f577d15a50 MD5 | raw file
  1. package babyselfie.mobi.bot;
  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.AsyncTask;
  7. import android.util.Log;
  8. import com.R;
  9. import babyselfie.mobi.Util.CameraUtil;
  10. import org.bytedeco.javacpp.avcodec;
  11. import org.bytedeco.javacpp.opencv_core;
  12. import org.bytedeco.javacv.FFmpegFrameRecorder;
  13. import org.bytedeco.javacv.Frame;
  14. import org.bytedeco.javacv.OpenCVFrameConverter;
  15. import java.io.File;
  16. import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
  17. /**
  18. * Created by Bot on 09/12/2016.
  19. */
  20. public class MovieCreator extends AsyncTask<String, Void, Boolean> {
  21. public MovieCreator(Context context) {
  22. this.context = context;
  23. this.contextApp = context.getApplicationContext();
  24. }
  25. private final Context context;
  26. private final Context contextApp;
  27. private ProgressDialog progressDialog;
  28. private int imageHeight;
  29. private int imageWidth;
  30. protected String createMovie() {
  31. // File folderAll = Environment
  32. // .getExternalStorageDirectory();
  33. File folderBabySelfie = CameraUtil.getGalleryDirectory();
  34. // File folderMovies = Environment
  35. // .getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
  36. String path = CameraUtil.getVideosDirectory().getPath();
  37. File[] listOfFiles = folderBabySelfie.listFiles();
  38. int imgCounter = listOfFiles.length;
  39. Log.v("pathReal", path);
  40. FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("file://" + path + "/video.mp4", 768, 1024);
  41. OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
  42. try {
  43. recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
  44. recorder.setVideoOption("preset", "ultrafast");
  45. recorder.setVideoQuality(10);
  46. recorder.setFormat("mp4");
  47. recorder.setFrameRate(1);
  48. recorder.setPixelFormat(org.bytedeco.javacpp.avutil.AV_PIX_FMT_YUV420P);
  49. recorder.start();
  50. // long imageTime = 50000000;
  51. for (int i = 0; i < imgCounter; i++) {
  52. opencv_core.IplImage img = cvLoadImage(listOfFiles[i].getAbsolutePath());
  53. // long time = (i+1) * imageTime;
  54. // if (time > recorder.getTimestamp()) {
  55. // recorder.setTimestamp(time);
  56. // }
  57. // recorder.setTimestamp(recorder.getTimestamp()+3000);
  58. Log.v("recordtimeStamp", recorder.getTimestamp() + "");
  59. Frame frame = converter.convert(img);
  60. recorder.record(frame);
  61. }
  62. recorder.stop();
  63. recorder.release();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. } finally {
  67. // TODO Fazer isso performatico, essa ñ é a forma recomendada
  68. //sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES))));
  69. }
  70. return "video.mp4";
  71. }
  72. @Override
  73. protected Boolean doInBackground(String... params) {
  74. String fileName = createMovie();
  75. boolean result = fileName.isEmpty();
  76. return true;
  77. }
  78. @Override
  79. protected void onPreExecute() {
  80. progressDialog = new ProgressDialog(context, R.style.AppTheme_Dark_Dialog);
  81. progressDialog.setTitle(contextApp.getString(R.string.carregando_video));
  82. progressDialog.setMessage(contextApp.getString(R.string.espere_por_favor));
  83. progressDialog.setCancelable(false);
  84. progressDialog.setIndeterminate(true);
  85. progressDialog.show();
  86. }
  87. @Override
  88. protected void onPostExecute(Boolean result) {
  89. if (progressDialog != null) {
  90. progressDialog.dismiss();
  91. }
  92. Intent it = new Intent(context,Tela9VideoActivity.class);
  93. Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  94. mediaStoreUpdateIntent.setData(Uri.parse("file://" + CameraUtil.getVideosDirectory().getPath() + "video.mp4"));
  95. context.sendBroadcast(mediaStoreUpdateIntent);
  96. context.startActivity(it);
  97. }
  98. }