Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active February 26, 2024 22:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vxhviet/cd39621ae768be07dca30e399c36c034 to your computer and use it in GitHub Desktop.
Save vxhviet/cd39621ae768be07dca30e399c36c034 to your computer and use it in GitHub Desktop.
How to fucking add JavaCV to Android Studio

Source: Me, GitHub, following this for update GitHub

Question: How to fucking add JavaCV to Android Studio so I can use the fucking annoying FFmpeg?

Answer: This tut will use JavaCV 1.1, version 1.2 currently has SIGSEGV issue. JavaCV 1.1 comes with FFmpeg 2.8.1.

  1. Obtain the prebulit binaries here.
  2. Extract it and copy these files:
  • javacpp.jar (essential).
  • javacv.jar (essential).
  • ffmpeg.jar (essential if you use FFmpeg).
  • ffmpeg-android-arm.jar (for arm CPU).
  • ffmpeg-android-x86.jar (optional for x86 CPU). into the libs folder of your project (app/libs in my case).
  1. Click on app -> F4 -> Dependencies -> + -> File dependencies -> choose all the previous .jar.
  2. Temporarily, because of this issue, we need to set targetSdkVersion 22 the app's build.gradle.
  3. Possibly need to set these packagingOptions in the build.gradle:
android {
  compileSdkVersion 23
  buildToolsVersion "23.0.3"

  defaultConfig {
      applicationId "com.example.test"
      minSdkVersion 16
      targetSdkVersion 22
      versionCode 1
      versionName "1.0"
  }
  
  packagingOptions {
      exclude 'META-INF/LICENSE.txt'
      exclude 'META-INF/NOTICE.txt'
      exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
      exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'
      
      //might need these if you use openCV
      //exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
      //exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
  }
}

To play overlay at different time, see second link.

@Nitinvermaa1
Copy link

Hi,
I come across below error when i am trying to access OEM device (EasyCap) to grab frame form Set top box
Code:
FFmpegFrameGrabber grabber =new FFmpegFrameGrabber("video="OEM Device":audio="OEM Device"");

grabber.setFormat("dshow");
grabber.setImageWidth(640);
grabber.setImageHeight(480);
grabber.start();

CanvasFrame frame = new CanvasFrame("Screen Capture");
while (frame.isVisible()) {
frame.showImage(grabber.grab());
}
frame.dispose();
grabber.stop();
Error:
Exception in thread "main" org.bytedeco.javacv.FrameGrabber$Exception: avformat_open_input() error -5: Could not open input "video="OEM Device":audio="OEM Device"". (Has setFormat() been called?)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:429)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:377)
at stbTesting.FFmpegEasyCap.main(FFmpegEasyCap.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
[dshow @ 000000000050c760] Could not find video device with name ["OEM Device"] among source devices of type video.
[dshow @ 000000000050c760] Could not find video device with name ["OEM Device"] among source devices of type video.

Please help me out to resolve the above issue
From command line using ffmpeg i am able to play as well as record the Set top box video using easycap

For Direct Play:

ffplay -f dshow -crossbar_video_input_pin_number 1 -crossbar_audio_input_pin_number 2 -rtbufsize 500000k -framerate 29.97 -i video="OEM Device":audio="OEM Device"

For Recording:

ffmpeg -f dshow -crossbar_video_input_pin_number 1 -crossbar_audio_input_pin_number 2 -rtbufsize 500000k -framerate 29.97 -i video="OEM Device":audio="OEM Device" C:\stbtestVideo\Test.avi

@devKarpov
Copy link

devKarpov commented Feb 26, 2024

This worked to get javacv and ffmpeg to work for me! However I was using Kotlin and had to do some other steps. Instead of 3 I used implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar")))) in the build.gradle file.

I also had to add android:extractNativeLibs="true" to the application part AndroidManifest.xml. After that it worked.
It might not even be needed to download the jars anymore and just import packages from Gradle directly. I think the modification to the xml was the key part for it to work. But following these steps worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment