Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

trimming videos

trimming videos

a flutter package for trimming videos.

features

trimming videos

  • customizable video trimmer
  • video playback control
  • retrieving and storing video file

also, supports conversion to gif.

trim editor

editor_demo

example app

trimming videos

customizable video editor

trim_editor

usage

trimming videos

  • add the dependency video_trimmer to your pubspec.yaml file.

android

trimming videos

  • go to <project root>/android/app/build.gradle and set the proper minsdkversion, 24 for main release or 16 for lts release.

    refer to the ffmpeg release section.

    minsdkversion <version>
    
  • go to <project root>/android/build.gradle and add the following line:
    ext.flutterffmpegpackage = '<package name>'
    

    replace the <package name> with a proper package name from the packages list section.

ios

trimming videos

  • add the following keys to your info.plist file, located in <project root>/ios/runner/info.plist:
    <key>nscamerausagedescription</key>
    <string>used to demonstrate image picker plugin</string>
    <key>nsmicrophoneusagedescription</key>
    <string>used to capture audio for image picker plugin</string>
    <key>nsphotolibraryusagedescription</key>
    <string>used to demonstrate image picker plugin</string>
    
  • set the platform version in ios/podfile, 11.0 for main release or 9.3 for lts release.

    refer to the ffmpeg release section.

    platform :ios, '<version>'
    
  • [flutter >= 1.20.x] edit ios/podfile and add the following block before target 'runner' do section:
    def flutter_install_ios_plugin_pods(ios_application_path = nil)
      # defined_in_file is set by cocoapods and is a pathname to the podfile.
      ios_application_path ||= file.dirname(defined_in_file.realpath) if self.respond_to?(:defined_in_file)
      raise 'could not find ios application path' unless ios_application_path
    
      # prepare symlinks folder. we use symlinks to avoid having podfile.lock
      # referring to absolute paths on developers' machines.
    
      symlink_dir = file.expand_path('.symlinks', ios_application_path)
      system('rm', '-rf', symlink_dir) # avoid the complication of dependencies like fileutils.
    
      symlink_plugins_dir = file.expand_path('plugins', symlink_dir)
      system('mkdir', '-p', symlink_plugins_dir)
    
      plugins_file = file.join(ios_application_path, '..', '.flutter-plugins-dependencies')
      plugin_pods = flutter_parse_plugins_file(plugins_file)
      plugin_pods.each do |plugin_hash|
        plugin_name = plugin_hash['name']
        plugin_path = plugin_hash['path']
        if (plugin_name && plugin_path)
          symlink = file.join(symlink_plugins_dir, plugin_name)
          file.symlink(plugin_path, symlink)
    
          if plugin_name == 'flutter_ffmpeg'
              pod 'flutter_ffmpeg/<package name>', :path => file.join('.symlinks', 'plugins', plugin_name, 'ios')
          else
              pod plugin_name, :path => file.join('.symlinks', 'plugins', plugin_name, 'ios')
          end
        end
      end
    end
    

    replace the <package name> with a proper package name from the packages list section.

  • [flutter < 1.20.x] edit ios/podfile file and modify the default # plugin pods block as follows.
    # prepare symlinks folder. we use symlinks to avoid having podfile.lock
    # referring to absolute paths on developers' machines.
    
    system('rm -rf .symlinks')
    system('mkdir -p .symlinks/plugins')
    plugin_pods = parse_kv_file('../.flutter-plugins')
    plugin_pods.each do |name, path|
      symlink = file.join('.symlinks', 'plugins', name)
      file.symlink(path, symlink)
      if name == 'flutter_ffmpeg'
          pod name+'/<package name>', :path => file.join(symlink, 'ios')
      else
          pod name, :path => file.join(symlink, 'ios')
      end
    end
    

    replace the <package name> with a proper package name from the packages list section.

ffmpeg release

trimming videos

in reference to the releases specified in the flutter_ffmpeg package.

main release lts release
android api level 24 16
android camera access yes
android architectures arm-v7a-neon
arm64-v8a
x86
x86-64
arm-v7a
arm-v7a-neon
arm64-v8a
x86
x86-64
xcode support 10.1 7.3.1
ios sdk 12.1 9.3
ios architectures arm64
arm64e
x86-64
armv7
arm64
i386
x86-64

packages list

trimming videos

the following ffmpeg packages list is in reference to the flutter_ffmpeg package.

package main release lts release
min min min-lts
min-gpl min-gpl min-gpl-lts
https https https-lts
https-gpl https-gpl https-gpl-lts
audio audio audio-lts
video video video-lts
full full full-lts
full-gpl full-gpl full-gpl-lts

functionalities

trimming videos

loading input video file

final trimmer _trimmer = trimmer();
await _trimmer.loadvideo(videofile: file);

saving trimmed video

returns a string to indicate whether the saving operation was successful.

await _trimmer
    .savetrimmedvideo(startvalue: _startvalue, endvalue: _endvalue)
    .then((value) {
  setstate(() {
    _value = value;
  });
});

video playback state

returns the video playback state. if true then the video is playing, otherwise it is paused.

await _trimmer.videplaybackcontrol(
  startvalue: _startvalue,
  endvalue: _endvalue,
);

advanced command

you can use an advanced ffmpeg command if you require more customization. just define your ffmpeg command using the ffmpegcommand property and set an output video format using customvideoformat.

refer to the official ffmpeg documentation for more information.

note: passing a wrong video format to the customvideoformat property may result in a crash.

// example of defining a custom command

// this is already used for creating gif by
// default, so you do not need to use this.

await _trimmer
    .savetrimmedvideo(
        startvalue: _startvalue,
        endvalue: _endvalue,
        ffmpegcommand:
            '-vf "fps=10,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0',
        customvideoformat: '.gif')
    .then((value) {
  setstate(() {
    _value = value;
  });
});

widgets

display a video playback area

videoviewer()

display the video trimmer area

trimeditor(
  viewerheight: 50.0,
  viewerwidth: mediaquery.of(context).size.width,
  onchangestart: (value) {
    _startvalue = value;
  },
  onchangeend: (value) {
    _endvalue = value;
  },
  onchangeplaybackstate: (value) {
    setstate(() {
      _isplaying = value;
    });
  },
)

example

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:video_trimmer/trim_editor.dart';
import 'package:video_trimmer/video_trimmer.dart';
import 'package:video_trimmer/video_viewer.dart';

void main() => runapp(myapp());

class myapp extends statelesswidget {
  @override
  widget build(buildcontext context) {
    return materialapp(
      title: 'video trimmer',
      theme: themedata(
        primaryswatch: colors.blue,
      ),
      home: homepage(),
    );
  }
}

class homepage extends statelesswidget {
  final trimmer _trimmer = trimmer();
  @override
  widget build(buildcontext context) {
    return scaffold(
      appbar: appbar(
        title: text("video trimmer"),
      ),
      body: center(
        child: container(
          child: raisedbutton(
            child: text("load video"),
            onpressed: () async {
              file file = await imagepicker.pickvideo(
                source: imagesource.gallery,
              );
              if (file != null) {
                await _trimmer.loadvideo(videofile: file);
                navigator.of(context)
                    .push(materialpageroute(builder: (context) {
                  return trimmerview(_trimmer);
                }));
              }
            },
          ),
        ),
      ),
    );
  }
}

class trimmerview extends statefulwidget {
  final trimmer _trimmer;
  trimmerview(this._trimmer);
  @override
  _trimmerviewstate createstate() => _trimmerviewstate();
}

class _trimmerviewstate extends state<trimmerview> {
  double _startvalue = 0.0;
  double _endvalue = 0.0;

  bool _isplaying = false;
  bool _progressvisibility = false;

  future<string> _savevideo() async {
    setstate(() {
      _progressvisibility = true;
    });

    string _value;

    await widget._trimmer
        .savetrimmedvideo(startvalue: _startvalue, endvalue: _endvalue)
        .then((value) {
      setstate(() {
        _progressvisibility = false;
        _value = value;
      });
    });

    return _value;
  }

  @override
  widget build(buildcontext context) {
    return scaffold(
      appbar: appbar(
        title: text("video trimmer"),
      ),
      body: builder(
        builder: (context) => center(
          child: container(
            padding: edgeinsets.only(bottom: 30.0),
            color: colors.black,
            child: column(
              mainaxisalignment: mainaxisalignment.center,
              mainaxissize: mainaxissize.max,
              children: <widget>[
                visibility(
                  visible: _progressvisibility,
                  child: linearprogressindicator(
                    backgroundcolor: colors.red,
                  ),
                ),
                raisedbutton(
                  onpressed: _progressvisibility
                      ? null
                      : () async {
                          _savevideo().then((outputpath) {
                            print('output path: $outputpath');
                            final snackbar = snackbar(content: text('video saved successfully'));
                            scaffold.of(context).showsnackbar(snackbar);
                          });
                        },
                  child: text("save"),
                ),
                expanded(
                  child: videoviewer(),
                ),
                center(
                  child: trimeditor(
                    viewerheight: 50.0,
                    viewerwidth: mediaquery.of(context).size.width,
                    onchangestart: (value) {
                      _startvalue = value;
                    },
                    onchangeend: (value) {
                      _endvalue = value;
                    },
                    onchangeplaybackstate: (value) {
                      setstate(() {
                        _isplaying = value;
                      });
                    },
                  ),
                ),
                flatbutton(
                  child: _isplaying
                      ? icon(
                          icons.pause,
                          size: 80.0,
                          color: colors.white,
                        )
                      : icon(
                          icons.play_arrow,
                          size: 80.0,
                          color: colors.white,
                        ),
                  onpressed: () async {
                    bool playbackstate =
                        await widget._trimmer.videplaybackcontrol(
                      startvalue: _startvalue,
                      endvalue: _endvalue,
                    );
                    setstate(() {
                      _isplaying = playbackstate;
                    });
                  },
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.