Home > database >  Flutter: get the length/duration of a mp4 file
Flutter: get the length/duration of a mp4 file

Time:07-23

i have the difficulty to read the metadata from a mp4 file. i want to know how long the video goes in seconds.

This file is located under assets/ to be precise the path is assets/test.mp4

  int getLength() {
    var file = File('./assets/test.mp4');

    // get the length
    var length;

    return length;
  }

CodePudding user response:

You can use the video player plugin and use this code

https://pub.dev/packages/video_player

VideoPlayerController controller = VideoPlayerController.file('fileNameHere');
await controller.initialize();

print(controller.value.duration);

CodePudding user response:

Use this plugin flutter_ffmpeg: ^0.4.2

print("Duration: ${info.getMediaProperties()['duration']}");

final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();

 _flutterFFprobe.getMediaInformation("<file path or uri>").then((info) {
     print("Media Information");

     print("Path: ${info.getMediaProperties()['filename']}");
     print("Format: ${info.getMediaProperties()['format_name']}");
     print("Duration: ${info.getMediaProperties()['duration']}");
     print("Start time: ${info.getMediaProperties()['start_time']}");
     print("Bitrate: ${info.getMediaProperties()['bit_rate']}");
     Map<dynamic, dynamic> tags = info.getMediaProperties()['tags'];
     if (tags != null) {
         tags.forEach((key, value) {
             print("Tag: "   key   ":"   value   "\n");
         });
     }

     if (info.getStreams() != null) {
         List<StreamInformation> streams = info.getStreams();

         if (streams.length > 0) {
             for (var stream in streams) {
                 print("Stream id: ${stream.getAllProperties()['index']}");
                 print("Stream type: ${stream.getAllProperties()['codec_type']}");
                 print("Stream codec: ${stream.getAllProperties()['codec_name']}");
                 print("Stream full codec: ${stream.getAllProperties()['codec_long_name']}");
                 print("Stream format: ${stream.getAllProperties()['pix_fmt']}");
                 print("Stream width: ${stream.getAllProperties()['width']}");
                 print("Stream height: ${stream.getAllProperties()['height']}");
                 print("Stream bitrate: ${stream.getAllProperties()['bit_rate']}");
                 print("Stream sample rate: ${stream.getAllProperties()['sample_rate']}");
                 print("Stream sample format: ${stream.getAllProperties()['sample_fmt']}");
                 print("Stream channel layout: ${stream.getAllProperties()['channel_layout']}");
                 print("Stream sar: ${stream.getAllProperties()['sample_aspect_ratio']}");
                 print("Stream dar: ${stream.getAllProperties()['display_aspect_ratio']}");
                 print("Stream average frame rate: ${stream.getAllProperties()['avg_frame_rate']}");
                 print("Stream real frame rate: ${stream.getAllProperties()['r_frame_rate']}");
                 print("Stream time base: ${stream.getAllProperties()['time_base']}");
                 print("Stream codec time base: ${stream.getAllProperties()['codec_time_base']}");

                 Map<dynamic, dynamic> tags = stream.getAllProperties()['tags'];
                 if (tags != null) {
                   tags.forEach((key, value) {
                     print("Stream tag: "   key   ":"   value   "\n");
                   });
                 }
             }
         }
     }
 });
  • Related