Get frames in a video in Sketchware

 This sample shows how to get the frames in a video and display it in a GridView in Sketchware.

1. Create a new project in Sketchware.

2. In main.xml add a LinearLayout linear1. Below this add a ListView listview1.

3. Create a more block custom.xml. Add an ImageView imageview1 in it.

4. For listview1, select custom.xml as Custom View. Set height of linear1 to match_parent.

5. In View main.xml add a Floating Action Button _fab.

6. Add a FilePicker Component fp: video/*, and a Camera component cam.

7. Create a More block extra and declare GridView gridview1 and a list of bitmap images frames using following codes:

} GridView gridview1;

ArrayList<Bitmap> frames; {

8. Create a String path, a Map variable map, a List String list and a List Map imagelist.

9. In onCreate event put following codes.

gridview1 = new GridView(this);

gridview1.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.MATCH_PARENT));

gridview1.setBackgroundColor(Color.WHITE);

gridview1.setNumColumns(2); gridview1.setColumnWidth(GridView.AUTO_FIT);

gridview1.setVerticalSpacing(16); gridview1.setHorizontalSpacing(2); gridview1.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

linear1.removeAllViews();

linear1.addView(gridview1);

gridview1.setAdapter(new Listview1Adapter(imagelist));

((BaseAdapter)gridview1.getAdapter()).notifyDataSetChanged();

10. In fab onClick event, use File Picker fp pick files block.

11. In File Picker onFilesPicked event, use following codes:

path = _filePath.get((int)(0));

java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newSingleThreadExecutor();

final Handler handler = new Handler(Looper.getMainLooper());

executor.execute(new Runnable() {

@Override

public void run() {

//Background work here

imagelist.clear();

MediaMetadataRetriever retriever = new MediaMetadataRetriever();

retriever.setDataSource(getApplicationContext(), FileProvider.getUriForFile(MainActivity.this, getApplicationContext().getPackageName() + ".provider", new java.io.File(path)));

String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);

int millSecond = Integer.parseInt(duration);

int duration_second = millSecond / 1000;

frames = new ArrayList<>();

int numeroFrameCaptured = (int)(duration_second*2);


for (int i = 0; i < numeroFrameCaptured; i++){

frames.add(retriever.getFrameAtTime((duration_second*1000000*i)/numeroFrameCaptured));

map = new HashMap<>();

map.put("frame", "new");

imagelist.add(map);

}

handler.post(new Runnable() {

@Override

public void run() {

//UI Thread work here

gridview1.setAdapter(new Listview1Adapter(imagelist));

((BaseAdapter)gridview1.getAdapter()).notifyDataSetChanged();

} });

} });

The code in blue above is for doing the task of retrieving frames in background thread.

12. In ListView onBindCustomView event, display bitmap from bitmap list frames, in imageview1.

imageview1.setImageBitmap(frames.get(_position));

13. Run the project.

Watch video below



Comments

  1. I don't edit videos much. Time consuming. However this is a useful tutorial.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Wow this is a very cool tutorial. Pls sir can you help us make a tutorial on how to enable sav webpage in webview ( like save for offline pages) pls.

    ReplyDelete
  4. Use a good camera, do a pre-record sound test, and learn some simple editing skills if you can’t afford to higher a video editor. albanische hits 2022

    ReplyDelete
  5. Read file path is not working! How to do?

    ReplyDelete
  6. Please help me.

    After I run to apk and I recheck source codes ,real Admob units are right units.

    When I export to .aab and I recheck the source codes, real Admob units change to test units automatically .

    How to fix this error?

    Thank you.

    ReplyDelete
  7. melodyloops.com doubles as a fantastic resource for anyone who wants to download free music. The site is primarily aimed at new bands; you're not going to find tracks from the current chart-toppers.

    ReplyDelete
  8. I want to know how to create an appwidget with sketchware. Help me please

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware