视频剪辑后如何压缩

问题描述 投票:0回答:1

我正在开发flutter应用程序并尝试使用

video_trimmer:
修剪后压缩视频,但它不接受压缩

 uploadVideo(int data)async { 
await _trimmer.saveTrimmedVideo(startValue: _startValue, endValue: _endValue ) // if i removed this function it will compress perfectly 
    .then((value) async {
  setState(() => file = File(value));
// the path into value is '/data/user/0/com.example.whosaround/app_flutter/Trimmer/WhatsApp_Video_2021-07-01_at_11_trimmed:Jul3,2021-08:27:59.mp4'
});

// compress Video .. will not compress 
  final info = await VideoCompress.compressVideo(
    file.path, 
    quality: VideoQuality.LowQuality,
    deleteOrigin: false,
    includeAudio: true,
  );
  //refrech file video after compressed
  setState(() => file = File(file.path));
}

  //but will send normally to storage ! 
    if (file == null) return;
    final fileName = basename(file.path);
    final destination = 'files/$fileName';
    task = FirebaseApi.uploadFile( destination,file);
    setState(() {});



}
flutter file dart video compression
1个回答
1
投票
  XFile? imageFile = await ImagePicker().pickVideo(
        source: ImageSource.gallery);
    if (imageFile != null) {
      debugPrint("Selected video => ${imageFile.path}");
      File file = File(imageFile.path);
      var data = await Navigator.of(context).push(
        MaterialPageRoute(builder: (context) {
          return TrimmerView(file);
        }),
      );

      if (data != null) {
        callback.onSuccessTrimVideoFile(data, "video");
      }
    } else {
      debugPrint('No video selected.');
    }

您可以使用此方法进行视频缩略图和压缩

 void onSuccessTrimVideoFile(String file, String fileType) {
    Directory appDocDir = await getTemporaryDirectory();
        String appDocPath = appDocDir.path;
        String newPath = path.join(appDocPath, '${getRandomString(10)}.mp4');
        File filepath = await File(file).copy(newPath);
    
     final thumbnailFile =await VideoCompress.getFileThumbnail(filepath.path, quality: 50);
          MediaInfo? mediaInfo = await VideoCompress.compressVideo(
            filepath.path,
            quality: VideoQuality.LowQuality,
            deleteOrigin: false, // It's false by default
          );
  }
 

String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
      length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));

您可以使用此方法进行视频缩略图和压缩

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:video_trimmer/video_trimmer.dart';

class TrimmerView extends StatefulWidget {
  final File file;

  const TrimmerView(this.file, {Key? key}) : super(key: key);
  @override
  State<TrimmerView> createState() => _TrimmerViewState();
}

class _TrimmerViewState extends State<TrimmerView> {
  final Trimmer _trimmer = Trimmer();

  double _startValue = 0.0;
  double _endValue = 0.0;

  bool _isPlaying = false;
  bool _progressVisibility = false;

  @override
  void initState() {
    super.initState();
    _loadVideo();
  }

  void _loadVideo() {
    _trimmer.loadVideo(videoFile: widget.file);
  }

  _saveVideo() {
    setState(() {
      _progressVisibility = true;
    });

    _trimmer.saveTrimmedVideo(
      startValue: _startValue,
      endValue: _endValue,
      onSave: (outputPath) {
        setState(() {
          _progressVisibility = false;
        });
        debugPrint('OUTPUT PATH: $outputPath');
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (Navigator.of(context).userGestureInProgress) {
          return false;
        } else {
          return true;
        }
      },
      child: Scaffold(
        backgroundColor: Colors.black,
        appBar: AppBar(
          title: const Text("Video Trimmer"),
        ),
        body: Center(
          child: Container(
            padding: const EdgeInsets.only(bottom: 30.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Visibility(
                  visible: _progressVisibility,
                  child: const LinearProgressIndicator(
                    backgroundColor: Colors.red,
                  ),
                ),

                Expanded(
                  child: VideoViewer(trimmer: _trimmer),
                ),
                Center(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: TrimViewer(
                      trimmer: _trimmer,
                      viewerHeight: 50.0,
                      viewerWidth: MediaQuery.of(context).size.width,
                      durationStyle: DurationStyle.FORMAT_MM_SS,
                      maxVideoLength: const Duration(seconds: 30),
                      editorProperties: TrimEditorProperties(
                        borderPaintColor: Colors.yellow,
                        borderWidth: 4,
                        borderRadius: 5,
                        circlePaintColor: Colors.yellow.shade800,
                      ),
                      areaProperties: TrimAreaProperties.edgeBlur(
                        thumbnailQuality: 10,
                      ),
                      onChangeStart: (value) => _startValue = value,
                      onChangeEnd: (value) => _endValue = value,
                      onChangePlaybackState: (value) =>
                          setState(() => _isPlaying = value),
                    ),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Spacer(),
                    TextButton(
                      child: _isPlaying
                          ? const Icon(
                        Icons.pause,
                        size: 80.0,
                        color: Colors.white,
                      )
                          : const Icon(
                        Icons.play_arrow,
                        size: 80.0,
                        color: Colors.white,
                      ),
                      onPressed: () async {
                        bool playbackState = await _trimmer.videoPlaybackControl(
                          startValue: _startValue,
                          endValue: _endValue,
                        );
                        setState(() => _isPlaying = playbackState);
                      },
                    ),
                    Spacer(),
                    ElevatedButton(
                      onPressed: _progressVisibility ? null : () => _saveVideo(),
                      child: const Text("SAVE"),
                    ),
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.