I am developing an application that takes a local video file and extracts its frames and write on disk. I am using ffmpegmediametadataretriever
to extract frames from videos. I have done the following code
retriever.setDataSource(activity, uri);
Log.e("duration -> ", retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
long duration = Long.parseLong(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
int everyNFrame = 1;
double frameRate = Double.parseDouble(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_FRAMERATE));
Log.e("all metadata", retriever.getMetadata().getAll().toString());
long sec = Math.round(1000 * 1000 / (frameRate));
Bitmap bitmap;
// Bitmap bitmap2;
// Log.e(" timeskip ", sec + " ----------- " + (frameRate * 1000));
for (long i = 1000; i < duration * 1000; i += sec)
// for (long i = sec; i < duration * 1000 && !stopWorking; i += sec)//30*sec)
// for(int i=1000000;i<duration*1000 && !stopWorking;i+=300000)
{
bitmap = retriever.getFrameAtTime(i, FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC);
if (bitmap != null) {
try {
FileOutputStream out = new FileOutputStream(path + "/img_" + (i) + ".jpg");
Log.e("filename->", path + "/img_" + i + ".jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
bitmap.recycle();
Thread.sleep(75);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
It is not extracting all the frames from the video. for some videos frames extracted are repeated. Random number of frames are extracted for different videos. for some videos, 70-80% frames are extracted and for some only 15-20 frames are extracted.
I have gone through all the answers I could find on StackOverflow and other websites to find a solution but the issue is there.