flutter开发实战-实现获取视频的缩略图封面video_thumbnail

news2025/6/12 16:43:12

flutter开发实战-实现获取视频的缩略图封面video_thumbnail

在很多时候,我们查看视频的时候,视频没有播放时候,会显示一张封面,可能封面没有配置图片,这时候就需要通过获取视频的缩略图来显示封面了。这里使用了video_thumbnail来实现获取视频的缩略图。

一、引入video_thumbnail

在工程的pubspec.yaml中引入插件

  # 视频缩略图
  video_thumbnail: ^0.5.3
    

VideoThumbnail的属性如下

static Future<String?> thumbnailFile(
      {required String video,
      Map<String, String>? headers,
      String? thumbnailPath,
      ImageFormat imageFormat = ImageFormat.PNG,
      int maxHeight = 0,
      int maxWidth = 0,
      int timeMs = 0,
      int quality = 10}) 
    
  • thumbnailPath为本地存储的文件目录
  • imageFormat格式 jpg,png等
  • video视频地址
  • timeMs

二、获取视频的缩略图

使用video_thumbnail来获取视频缩略图

定义视频缩略图信息

class VideoThumbInfo {
  String url; // 原视频地址
  File? thumbFile; // 缩略图本地file
  int? width; // 缩略图的width
  int? height; // 缩略图的height

  VideoThumbInfo({
    required this.url,
  });
}
    

获取视频缩略图本地File

String path = (await getTemporaryDirectory()).path;
    String thumbnailPath = path + "/${DateTime.now().millisecond}.jpg";
    final fileName = await VideoThumbnail.thumbnailFile(
      video:
          "https://vd2.bdstatic.com/mda-maif0tt1rirqp27q/540p/h264_cae/1611052585/mda-maif0tt1rirqp27q.mp4",
      thumbnailPath: thumbnailPath,
      imageFormat: imageFormat,
      quality: quality,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      timeMs: timeMs,
    );

    File file = File(thumbnailPath);
    

获取缩略图的宽高

Image image = Image.file(thumbFile!);
      image.image.resolve(const ImageConfiguration()).addListener(
            ImageStreamListener(
              (ImageInfo imageInfo, bool synchronousCall) {
                int imageWidth = imageInfo.image.width;
                int imageHeight = imageInfo.image.height;
                VideoThumbInfo videoThumbInfo = VideoThumbInfo(url: url);
                videoThumbInfo.thumbFile = thumbFile;
                videoThumbInfo.width = imageWidth;
                videoThumbInfo.height = imageHeight;
                VideoThumb.setThumbInfo(url, videoThumbInfo);
                onVideoThumbInfoListener(videoThumbInfo);
              },
              onError: (exception, stackTrace) {
                print(
                    "getVideoThumbInfoByFile imageStreamListener onError exception:${exception.toString()},stackTrace:${stackTrace}");
                onVideoThumbInfoListener(null);
              },
            ),
          );
    

完整代码如下

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

// ignore: non_constant_identifier_names
VideoThumbManager get VideoThumb => VideoThumbManager.instance;

class VideoThumbManager {
  static VideoThumbManager get instance {
    return _singleton;
  }

  //保存单例
  static VideoThumbManager _singleton = VideoThumbManager._internal();

  //工厂构造函数
  factory VideoThumbManager() => _singleton;

  //私有构造函数
  VideoThumbManager._internal();

  // 保存url对应的本地缩略图file
  final _thumbMap = Map<String, File>();

  // url对应本地缩略图的信息
  final _thumbInfoMap = Map<String, VideoThumbInfo>();

  Future<void> setThumb(String url, File file) async {
    if (url.isEmpty) {
      return;
    }

    bool exist = await file.exists();
    if (exist == false) {
      return;
    }

    _thumbMap[url] = file;
  }

  Future<File?> getThumb(
    String url, {
    ImageFormat imageFormat = ImageFormat.JPEG,
    int maxHeight = 0,
    int maxWidth = 0,
    int timeMs = 0,
    int quality = 100,
  }) async {
    File? thumbFile = _thumbMap[url];
    if (thumbFile != null) {
      return thumbFile;
    }

    String path = (await getTemporaryDirectory()).path;
    String thumbnailPath = path + "/${DateTime.now().millisecond}.jpg";
    final fileName = await VideoThumbnail.thumbnailFile(
      video:
          "https://vd2.bdstatic.com/mda-maif0tt1rirqp27q/540p/h264_cae/1611052585/mda-maif0tt1rirqp27q.mp4",
      thumbnailPath: thumbnailPath,
      imageFormat: imageFormat,
      quality: quality,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      timeMs: timeMs,
    );

    File file = File(thumbnailPath);
    setThumb(url, file);
    return file;
  }

  // 获取缩略图的大小
  void getVideoThumbInfo(
    String url, {
    ImageFormat imageFormat = ImageFormat.JPEG,
    int maxHeight = 0,
    int maxWidth = 0,
    int timeMs = 0,
    int quality = 100,
    required Function(VideoThumbInfo?) onVideoThumbInfoListener,
  }) async {
    try {
      VideoThumbInfo? thumbInfo = VideoThumb.getThumbInfo(url);
      if (thumbInfo != null) {
        onVideoThumbInfoListener(thumbInfo);
        return;
      }

      await VideoThumb.getThumb(
        url,
        imageFormat: imageFormat,
        maxWidth: maxWidth,
        maxHeight: maxHeight,
        timeMs: timeMs,
        quality: quality,
      ).then((value) {
        File? thumbFile = value;
        if (thumbFile != null) {
          VideoThumb.getVideoThumbInfoByFile(
            url: url,
            thumbFile: thumbFile,
            onVideoThumbInfoListener: onVideoThumbInfoListener,
          );
        } else {
          onVideoThumbInfoListener(null);
        }
      }).onError((error, stackTrace) {
        print("getVideoThumbInfo error:${error.toString()}");
        onVideoThumbInfoListener(null);
      }).whenComplete(() {
        print("getVideoThumbInfo whenComplete");
      });
    } catch (e) {
      print("getVideoThumbInfo catch error:${e.toString()}");
      onVideoThumbInfoListener(null);
    }
  }

  /// 根据file获取缩略图信息
  void getVideoThumbInfoByFile({
    required String url,
    required File thumbFile,
    required Function(VideoThumbInfo?) onVideoThumbInfoListener,
  }) async {
    try {
      VideoThumbInfo? thumbInfo = VideoThumb.getThumbInfo(url);
      if (thumbInfo != null) {
        onVideoThumbInfoListener(thumbInfo);
        return;
      }

      Image image = Image.file(thumbFile!);
      image.image.resolve(const ImageConfiguration()).addListener(
            ImageStreamListener(
              (ImageInfo imageInfo, bool synchronousCall) {
                int imageWidth = imageInfo.image.width;
                int imageHeight = imageInfo.image.height;
                VideoThumbInfo videoThumbInfo = VideoThumbInfo(url: url);
                videoThumbInfo.thumbFile = thumbFile;
                videoThumbInfo.width = imageWidth;
                videoThumbInfo.height = imageHeight;
                VideoThumb.setThumbInfo(url, videoThumbInfo);
                onVideoThumbInfoListener(videoThumbInfo);
              },
              onError: (exception, stackTrace) {
                print(
                    "getVideoThumbInfoByFile imageStreamListener onError exception:${exception.toString()},stackTrace:${stackTrace}");
                onVideoThumbInfoListener(null);
              },
            ),
          );
    } catch (e) {
      print("getVideoThumbInfoByFile catch error:${e.toString()}");
      onVideoThumbInfoListener(null);
    }
  }

  void removeThumb(String url) {
    if (url.isEmpty) {
      return;
    }

    _thumbMap.remove(url);
  }

  /// 获取存储缩略图信息
  VideoThumbInfo? getThumbInfo(String url) {
    if (url.isEmpty) {
      return null;
    }

    VideoThumbInfo? thumbInfo = _thumbInfoMap[url];
    return thumbInfo;
  }

  /// 存储缩略图信息
  void setThumbInfo(String url, VideoThumbInfo videoThumbInfo) async {
    if (url.isEmpty) {
      return;
    }

    _thumbInfoMap[url] = videoThumbInfo;
  }

  void removeThumbInfo(String url) {
    if (url.isEmpty) {
      return;
    }

    _thumbInfoMap.remove(url);
  }

  void clear() {
    _thumbMap.clear();
    _thumbInfoMap.clear();
  }
}

class VideoThumbInfo {
  String url; // 原视频地址
  File? thumbFile; // 缩略图本地file
  int? width; // 缩略图的width
  int? height; // 缩略图的height

  VideoThumbInfo({
    required this.url,
  });
}

    

三、显示视频缩略图的Widget

用于显示视频缩略图的Widget

/// 用于显示视频缩略图的Widget
class VideoThumbImage extends StatefulWidget {
  const VideoThumbImage(
      {super.key, required this.url, this.maxWidth, this.maxHeight});

  final String url;
  final double? maxWidth;
  final double? maxHeight;

  @override
  State<VideoThumbImage> createState() => _VideoThumbImageState();
}

class _VideoThumbImageState extends State<VideoThumbImage> {
  VideoThumbInfo? _videoThumbInfo;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    VideoThumb.getVideoThumbInfo(widget.url,
        onVideoThumbInfoListener: (VideoThumbInfo? thumbInfo) {
      if (mounted) {
        setState(() {
          _videoThumbInfo = thumbInfo;
        });
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  // 根据VideoThumb来显示图片
  Widget buildVideoThumb(BuildContext context) {
    if (_videoThumbInfo != null && _videoThumbInfo!.thumbFile != null) {
      double? imageWidth;
      double? imageHeight;
      if (_videoThumbInfo!.width != null && _videoThumbInfo!.height != null) {
        imageWidth = _videoThumbInfo!.width!.toDouble();
        imageWidth = _videoThumbInfo!.height!.toDouble();
      }

      return Container(
        width: imageWidth,
        height: imageHeight,
        clipBehavior: Clip.hardEdge,
        decoration: const BoxDecoration(
          color: Colors.transparent,
        ),
        child: Image.file(
          _videoThumbInfo!.thumbFile!,
          width: imageWidth,
          height: imageHeight,
        ),
      );
    }

    return Container();
  }

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: widget.maxWidth ?? double.infinity,
        maxHeight: widget.maxHeight ?? double.infinity,
      ),
      child: buildVideoThumb(context),
    );
  }
}

    

效果图如下:

在这里插入图片描述

四、小结

flutter开发实战-实现获取视频的缩略图封面video_thumbnail

学习记录,每天不停进步。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1278031.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

mybatis源码(五)springboot pagehelper实现查询分页

1、背景 springboot的pagehelper插件能够实现对mybatis查询的分页管理&#xff0c;而且在使用时只需要提前声明即可&#xff0c;不需要修改已有的查询语句。使用如下&#xff1a; 之前对这个功能一直很感兴趣&#xff0c;但是一直没完整看过&#xff0c;今天准备详细梳理下。按…

阻抗匹配电阻原理及其应用

一、匹配电阻的作用 1、阻抗匹配 当信号频率比较高&#xff0c;上升沿比较陡时&#xff0c;电子信号经过阻抗不同的地方时也会产设反射。 PCB的单线阻抗一般会设计成50Ω&#xff0c;发射端阻抗一般是17到40&#xff0c;而接收端一般是MOS管的输入&#xff0c;阻抗是比较大的…

Linux ____04、文件内容查看(命令),网络配置(命令),软硬链接(命令)

文件内容查看&#xff0c;软硬链接 一、文件内容查看1、cat 由第一行开始显示文件内容&#xff0c;用来读文章&#xff0c;或者读取配置文件啊&#xff0c;都使用cat名2、tac 从最后一行开始显示&#xff0c;可以看出 tac 是 cat 的倒着写&#xff01;3、显示的时候&#xff0c…

C++基础 -33- 单目运算符重载

单目运算符重载格式 a和a通过形参确定 data1 operator() {this->a;return *this; }data1 operator(int) {data1 temp*this;this->a;return temp; }举例使用单目运算符重载 #include "iostream"using namespace std;class data1 {public :int a;data1(int…

linux复习笔记06(小滴)

演练企业静态ip地址配置过程 我们有时候会发现&#xff0c;在使用虚拟机的时候&#xff0c;如果使用远程连接工具&#xff0c;我们会发现&#xff0c;有时候连接不上去&#xff0c;但是我们去用ifconfig去查看的时候&#xff0c;我们发现是ip地址换了。所以往往我们也需要去固…

p标签在div中居中

新建一个html文件&#xff0c;命名为test.html&#xff0c;用于讲解如何在css中让div中的p标签居中。 在test.html文件内&#xff0c;在div内&#xff0c;使用p标签创建一行文字&#xff0c;用于测试。 在test.html文件内&#xff0c;设置div标签的class属性为mydiv。 在…

python绘制箱线图boxplot——用于多组数据的比较, 异常值检测

python绘制箱线图boxplot——用于多组数据的比较, 异常值检测 介绍箱线图方法简介箱线图适用范围seaborn.boxplot箱图外观设置异常值marker形状、填充色、轮廓设置完整代码 如下matplotlib.pyplot常见参数介绍 本文系统详解利用python中seaborn.boxplot绘制箱图boxplot。seab…

解决报错:error: (-215:Assertion failed) inv_scale_x > 0 in function ‘cv::resize‘

需求背景 欲使用opencv的resize函数将图像沿着纵轴放大一倍&#xff0c;即原来的图像大小为(384, 512), 现在需要将图像放大为(768, 512)。 源码 import cv2 import numpy as np# 生成初始图像 img np.zeros((384, 512), dtypenp.uint8) img[172:212, 32:-32] 255 H, W …

前端处理后端返回的字典值

<template><div></div> </template><script> export default {data () {return {data: {10: 北京,110: 山东},optionsData: []}},methods: {tranFn() {console.log(data>>>, this.data)const arr []for (let i 0; i < Object.keys…

分析实现HarmonyOS中的Linux内核架构模式

在当今的科技领域&#xff0c;操作系统是各种智能设备运行的关键所在。而在这方面&#xff0c;华为的鸿蒙系统备受瞩目。那么&#xff0c;鸿蒙系统技术架构是怎样的呢&#xff1f;本文将为您揭开这一神秘面纱。 首先&#xff0c;我们需要了解鸿蒙系统的基本架构。鸿蒙系统采用…

优先队列详解

优先队列是计算机科学中的一种抽象数据类型&#xff0c;它是一种队列&#xff1a;元素拥有优先级&#xff0c;优先级最高的元素最先得到服务&#xff1b;优先级相同的元素按照在集合中的顺序得到服务。优先队列有两种主要的实现方法&#xff1a;堆和二叉搜索树。 简单来说&…

vcruntime140.dll无法继续执行代码五种解决方法修复教程

在电脑使用过程中&#xff0c;我们可能会遇到一些常见的错误提示&#xff0c;其中之一就是“vcruntime140.dll丢失”。这个错误通常会导致某些应用程序无法正常运行。本文将介绍vcruntime140.dll丢失对电脑的影响以及如何修复这个问题&#xff0c;并提供一些预防措施&#xff0…

TCP/IP_整理起因

先分享一个初级的问题&#xff1b;有个客户现场&#xff0c;终端设备使用客户网络更新很慢&#xff0c;使用手机热点更新速度符合预期&#xff1b;网络部署情况如下&#xff1a; 前期花费了很大的精力进行问题排查对比&#xff0c;怀疑是客户网络问题&#xff08;其他的客户现…

Redis缓存的使用

什么是缓存 缓存就是数据交换的缓冲区&#xff0c;是存储数据的临时地方&#xff0c;一般读写性能较高。 缓存的作用&#xff1a; 降低后端负载提高读写效率&#xff0c;降低响应时间 缓存的成本&#xff1a; 数据一致性成本代码维护成本运维成本 Redis特点 键值型数据库…

使用JSP+Servlet+MySQL实现登录注册功能

✅作者简介&#xff1a;大家好&#xff0c;我是Leo&#xff0c;热爱Java后端开发者&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;Leo的博客 &#x1f49e;当前专栏&#xff1a; Java从入门到精通 ✨特色专栏&#xf…

java使用poi读写excel(处理上下标和科学计数法)

Background 要读写如下图所示的excel&#xff0c;符号和单位中包含上下标&#xff0c;在读写时需要特殊处理&#xff1b;取值列中是科学计数法&#xff0c;读写时需要特殊处理&#xff1b;excel中包含多个sheet&#xff0c;读的时候把所有sheet的数据读出来&#xff0c;写的时候…

Azure Machine Learning - 使用.NET创建和管理AI搜索功能

本文介绍了如何在 Azure SDK for .NET 中使用 C# 和 Azure.Search.Documents客户端库来创建和管理搜索对象。 关注TechLead&#xff0c;分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验&#xff0c;同济本复旦硕&#xff0c;复旦机器人智能实验室…

<Linux>(极简关键、省时省力)《Linux操作系统原理分析之linux存储管理(2)》(18)

《Linux操作系统原理分析之linux存储管理&#xff08;1&#xff09;》&#xff08;17&#xff09; 6 Linux存储管理6.2 选段符与段描述符6.2.1 选段符6.2.2 段描述符6.2.3 分段机制的存储保护 6.3 80x86 的分页机制6.3.180x86 的分页机制6.3.2 分页机制的地址转换6.3.3 页表目录…

uni-app 微信小程序 电子签名及签名图片翻转显示功能

文章目录 1. 需求背景2. 开始撸2.1 点击 重写 进入签名页面&#xff08;上图一&#xff09;2.2 书写签名&#xff0c;点击确认返回&#xff0c;及图片翻转显示&#xff08;上图二&#xff0c;三&#xff09; 3. 图片进行翻转&#xff0c;返回翻转后的图片 1. 需求背景 接的一个…

结构体||联合体

1.结构体 1.1实际生活中一些东西往往有多个元素组成。如一名学生有身高、体重、名字、学号等。这时候就需要用到结构体。 结构体是一些值的结合&#xff0c;这些值被称为成员变量。结构体的每个成员可以是不同类型的变量&#xff0c;如&#xff1a;标量、数组、指针、甚至是其…