1

For my app I need to load the individual images of a video file located in my res/raw/ directory into a list/array of images. From this list/array of images I need to pick some indecies, which will be stored into a new video video file also located in the res/raw/directory.

The problem is that I can not get the path to my video. If i try to use:

File f = new Flie("app/res/raw/test.mp4");

I get the error: the file can not be found.

I tried using Uri like:

String videopath ="android.resource://" + getPackageName()+ R.raw.test; File f = new Flie(videopath.toString());

But this does not work either.

Here is a pseudo code how I would need it:

List<Picture> video = new ArrayList<Picture>();
File file = new file("path_to_file/test.mp4");
FrameGrab grab = FrameGrab.createFrameGrab(NIOUtils.readableChannel(file));
Picture picture;

while (null != (picture = grab.getNativeFrame())) {
    video.add(picture);
}


List<picture> video_new = new ArrayList<picture>();
int[] idx = {1,2,4,6,8 ...}

for(int i= 0; i<idx.length; i++){
    picture= video.get(idx[i]);
    video_new.add(picture);
}

//stores the new video into the same path but with a different name
storefile("path_to_file/test_new.mp4", video_new);

Paul Koch
  • 11
  • 2

1 Answers1

0

It looks like you may be missing a slash in your file path.

Try changing:

String videopath ="android.resource://" + getPackageName()+ R.raw.test

To:

String videopath ="android.resource://" + getPackageName()+ "/" + R.raw.test

Using this to access and play a video would look like:

VideoView view = (VideoView)findViewById(R.id.videoView);
String videopath ="android.resource://" + getPackageName()+ "/" + R.raw.test
view.setVideoURI(Uri.parse(videopath));
view.start();

Note

To extract frames from a media file (e.g. a video) Android provides the MediaMetadataRetriever class

Mick
  • 24,231
  • 1
  • 54
  • 120
  • Thank you very much for your help. But i just forgot to add it in the pseudo code my bad. The problem is that the file system does not understand the path given as "android.resource://". Thanks for the help thou. – Paul Koch Apr 02 '19 at 19:52
  • "android.resource://" is actually just a string - I've updated the answer with the full URI showing how you use it to play a video as an example. – Mick Apr 03 '19 at 08:18
  • It seams like you misunderstood the question. I dont want to view a video. I need the frames. Thanks for the help thou. – Paul Koch Apr 03 '19 at 09:54
  • 'The problem is that I can not get the path to my video' - this is the problem you highlighted, I think. Android uses URI's to identify files so the example above is showing how to use a URI to access the file. If you want a solution to extract each frame from a video and store it then you probably want to include some actual code and highlight the part which is not working for you. I've added a note above about one way to get frames from a video file - you can also use OpenCv although the documentation is not great for Android Studio last time I looked. – Mick Apr 03 '19 at 10:41
  • The MediaMetadataRetriever is actually what I am looking for. I googled for two days and could not find any reference to it. Thank you very much. However, I found a different method myselv. I load the video from my resourses as inputstream and save that input stream to my internal storage. From that i can use the direct path to create a new file and use jcodec to read the frames. However, Ill have a look into the MediaMetadataRetriever. That seams to be the correct way. – Paul Koch Apr 03 '19 at 11:34