Ask Your Question
0

Loading video files using VideoCapture in Android

asked 2017-02-10 06:13:36 -0600

iuq gravatar image

Hi,

My aim is to load two video files, grabbing the frames from each file and creating a single frame.

I have understood I can use VideoCapture java class to load files and use Mat class to work on individual frames.

I have two AVI files stored at /storage/emulated/0/Download/received_files/<file>.avi. However VideoCapture is unable to open the files.

VideoCapture vc1 = new VideoCapture();
 VideoCapture vc2 = new VideoCapture();

 if (!vc1.open(video1)) {
    Log.e(TAG, "Could not open the video file1");
 } else {
    Log.i(TAG, "Video1 loaded");
 }

 if (!vc2.open(video1)) {
    Log.e(TAG, "Could not open the video file1");
 } else {
    Log.i(TAG, "Video2 loaded");
 }

It always pronts "Could not open ..." message. I have tested the file paths using File.exists and it returns true. What am I missing here?

I am using OpenCV 3.2.0 for Android and Android lollipop.

My Android setup is done properly and I can view the camera in JavaCameraView.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-02-10 06:35:52 -0600

berak gravatar image

unfortunately, you cannot load arbitrary video files on android, only MJPG codec in an avi container is supported from opencv (due to missing backend, like ffmpeg or gstreamer)

edit flag offensive delete link more

Comments

Thanks Berak for these pointer, I'll explore it more.

In fact, end product is stitching/merging multiple streams from cameras connected to an Android tablet via OTG USB hub. I am novice in this area and that is why I have picked up working with couple of offline videos first. I hope my approach is correct?

Great if you could share some links regrading opencv+ffmpeg on Android or anything related to my project! Thanks.

iuq gravatar imageiuq ( 2017-02-11 00:42:01 -0600 )edit

in that case, you might be better off, avoiding opencv, and using ffmpeg directly

berak gravatar imageberak ( 2017-02-11 00:52:20 -0600 )edit

seems ffmpeg can be used as a plugin to opencv!

iuq gravatar imageiuq ( 2017-02-11 06:20:50 -0600 )edit

@berak, I tried couple of videos both in AVI and MP4 format with respective extensions to no avail. I don't know how to put MPEG in AVI! I have tried this command ffmpeg -i input.avi -c:v mpeg4 -vtag xvid output.avi to convert the video but video was still not loaded by VideoCapture.

iuq gravatar imageiuq ( 2017-02-13 02:07:17 -0600 )edit

again, xvid codec is not supported natively. you need MJPG, as said before.

berak gravatar imageberak ( 2017-02-13 02:09:35 -0600 )edit

Got it finally! My source video is input.mp4. First, I converted that to MJPEG ffmpeg -i input.mp4 -vcodec mjpeg output.mjpeg and then ffmpeg -i output.mjpeg -vocdec output.avi. My bad, initially I read MJPG as MPEG in you reply! Thanks.

iuq gravatar imageiuq ( 2017-02-13 03:58:12 -0600 )edit

Hi, Could you please explain how do you run "ffmpeg -i input.mp4 -vcodec mjpeg output.mjpeg" in an Android app? Do you install ffmpeg on the device and run something like "Process process = Runtime.getRuntime().exec(commandLine);" in your code?

Milad gravatar imageMilad ( 2017-05-31 01:43:24 -0600 )edit

just in case: another way to convert MJPEg to AVI container avconv -i output.mjpeg -c:v copy -c:a copyoutput.avi

upd; even worse - not all MJPE\avi can be played by Videocapture - such file from Pentax DSLR (1536x1024) can't be opened.

ayaromenok gravatar imageayaromenok ( 2018-07-09 08:56:28 -0600 )edit
0

answered 2017-08-02 13:47:32 -0600

pkdos84 gravatar image

Hi Milad. There is a example of use in gitHub: https://github.com/WritingMinds/ffmpeg-android-java In this case use the command: execFFmpegBinary.

I hope this can help you.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-02-10 06:13:36 -0600

Seen: 11,800 times

Last updated: Aug 02 '17