Android Studio:从res / raw /目录中逐帧读取.mp4

时间:2019-04-02 14:05:20

标签: video android-resources android-studio-3.0 jcodec

对于我的应用程序,我需要将res / raw /目录中的视频文件的单个图像加载到图像列表/图像数组中。我需要从此列表/图像阵列中选择一些索引,这些索引将被存储到也位于res / raw /目录中的新视频文件中。

问题是我无法获取视频的路径。 如果我尝试使用:

文件f =新的Flie(“ app / res / raw / test.mp4”);

我得到了错误:找不到文件。

我尝试像使用Uri:

String videopath =“ android.resource://” + getPackageName()+ R.raw.test; 文件f =新的Flie(videopath.toString());

但这也不起作用。

这是我需要的伪代码:

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);

1 个答案:

答案 0 :(得分:0)

看起来您的文件路径中可能缺少斜线。

尝试更改:

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

收件人:

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

使用它来访问和播放视频看起来像:

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

注意

要从媒体文件(例如视频)中提取帧,Android提供了MediaMetadataRetriever类