As I'm fresher in android I did the image processing using OpenCV.Now I need to process the video that is I need to play the video using OpenCV. Now I need to play the video in my app using OpenCV I tried using the videocapture class but it's getting an error that provides some implementation.So please help me in playing the video in my app using OpenCV.
Asked
Active
Viewed 1,214 times
0
-
take a look here : https://stackoverflow.com/questions/17401852/open-video-file-with-opencv-java – batsheva Aug 31 '17 at 05:33
-
VideoCapture capture=new VideoCapture();//Here its expecting the argument capture.open("Vid.mp4"); I had seen that code but actually even the video is not opening for me. and it is not playing the video.Please help me in playing the video – Nischitha s Aug 31 '17 at 06:16
2 Answers
1
Just giving the file name as an argument to the capture.open()
wont work here, given that you are working on an android platform.
I took the below steps to resolve my problem, which is similar to yours.
- Placed the video file under res/raw.
- Copied the video file from res/raw to an app specific directory ( I copied it to an external one, and I did this in a different thread from the UI/main thread )
- Got the video file from res/raw as an InputStream via
openRawResource()
- Created an instance of File and created an instance of FileOutputStream based on it.
- Wrote the input stream to the output stream. You could use
transferTo(OutputStream out)
, however it is only available in Java 9+. You can also follow this post as well.
- Got the video file from res/raw as an InputStream via
- Used the Absolute path of the recently copied file as the argument, via
getAbsolutePath()
, in the VideoCapture Constructor or inopen(String FileName)
.
Note: I understand this question was asked 3 years ago; I'm posting this as I think this might be useful for those who stumble upon this issue.

Kalai
- 503
- 1
- 3
- 12
1
I tried following the steps given by @Kalai. It seems to work. I did not face any errors. Giving the code in the hope it could help someone.
I kept my video file in the raw directory.
override fun onCreate(savedInstanceState: Bundle?) {
...
if (!OpenCVLoader.initDebug())
Log.e("OpenCV", "Unable to load OpenCV!")
else
Log.d("OpenCV", "OpenCV loaded Successfully!")
val absFilePath = moveFileFromRawToApp()
openFileByOpenCV(absFilePath)
}
private fun moveFileFromRawToApp(): String {
val ins: InputStream = resources.openRawResource(R.raw.marvel)
val outFile = getAppSpecificDirectory("marvel.mp4")
val out = FileOutputStream(outFile)
val outputStream = ByteArrayOutputStream()
var buffer = ByteArray(1024)
var size = ins.read(buffer, 0, 1024)
while (size >= 0) {
outputStream.write(buffer, 0, size)
size = ins.read(buffer, 0, 1024)
}
ins.close()
buffer = outputStream.toByteArray()
out.write(buffer)
out.close()
return outFile.absolutePath
}
private fun getAppSpecificDirectory(filename: String): File {
val file = File(baseContext.filesDir, filename)
if (!file.exists()) {
file.createNewFile()
}
return file
}
private fun openFileByOpenCV(absFilePath: String) {
val videoCapture = VideoCapture()
videoCapture.open(absFilePath)
Toast.makeText(this, "OpenCV worked", Toast.LENGTH_LONG).show()
}
At the end you will have a video file in your mobile device data > data > [package name] > files > marvel.mp4

Rana Ranvijay Singh
- 6,055
- 3
- 38
- 54