You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter项目:录制时及已录视频调速功能实现求助

Solution for Adjusting Video Playback Speed in Flutter (During & After Recording)

Hey there! I get how frustrating it can be hunting for the right package when you're stuck on a feature. Let's break down two key scenarios you're dealing with—adjusting speed during recording (preview) and after recording (playback/export)—and walk through practical solutions for each.

1. Adjusting Playback Speed After Recording (Simple Playback)

If you just need to let users change the speed while playing back the recorded video (without modifying the original file), the video_player package is your best bet—it's official, stable, and directly supports playback rate control.

Step-by-Step Implementation:

  • Add the video_player dependency to your pubspec.yaml:
    dependencies:
      video_player: ^2.8.1
    
  • Initialize the video controller and use setPlaybackRate() to adjust speed:
    late VideoPlayerController _controller;
    
    void initVideo(String videoPath) {
      _controller = VideoPlayerController.file(File(videoPath))
        ..initialize().then((_) {
          // Update UI when video is ready
          setState(() {});
        });
    }
    
    // Call this method when user selects a speed (e.g., 0.5x, 1.0x, 2.0x)
    void setSpeed(double rate) {
      if (_controller.value.isInitialized) {
        _controller.setPlaybackRate(rate);
      }
    }
    
  • Add UI controls (like buttons or a slider) to let users trigger the setSpeed method.

Notes:

  • This works for both local files and network videos.
  • Supported rates vary slightly by platform, but 0.25x to 2.0x is generally safe across iOS and Android.

2. Exporting a Permanently Speed-Altered Video File

If users need to save the video with the adjusted speed (e.g., a slow-mo clip to share), you'll need to process the video file directly. The flutter_ffmpeg package is the go-to tool for this—it wraps FFmpeg, the industry-standard media processing tool.

Step-by-Step Implementation:

  • Add flutter_ffmpeg to your pubspec.yaml:
    dependencies:
      flutter_ffmpeg: ^0.4.0
    
  • Use FFmpeg commands to adjust the video speed (and sync audio to avoid desync):
    import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
    
    final FlutterFFmpeg _flutterFFmpeg = FlutterFFmpeg();
    
    void convertVideoSpeed(String inputPath, String outputPath, double speed) {
      // Calculate PTS (Presentation Timestamp) for video: lower value = faster speed
      double videoPts = 1 / speed;
      // Audio tempo: match video speed (FFmpeg's atempo supports 0.5x to 2.0x; for wider range, chain multiple atempo filters)
      double audioTempo = speed;
    
      String command = '-i $inputPath -filter:v "setpts=$videoPts*PTS" -filter:a "atempo=$audioTempo" $outputPath';
    
      _flutterFFmpeg.execute(command).then((rc) {
        if (rc == 0) {
          print("Speed conversion successful!");
          // Handle the output file here (e.g., show it to the user)
        } else {
          print("Error occurred during conversion: $rc");
        }
      });
    }
    

Notes:

  • For speeds outside 0.5x-2.0x, use multiple atempo filters (e.g., atempo=2.0,atempo=2.0 for 4x speed).
  • Make sure to request storage permissions if you're saving the output file to external storage.

3. Adjusting Speed During Recording (Preview Only)

If you want users to see a slow-mo or fast-mo preview while recording, this is trickier because the camera package's default preview streams at the camera's native frame rate. Here's a workaround:

  • Use the camera's startImageStream to capture individual frames.
  • Control how often you render these frames to the UI to simulate speed changes. For example:
    • For 0.5x speed: Render every other frame (effectively halving the frame rate).
    • For 2x speed: Skip frames to render twice as fast.

Example snippet:

late CameraController _cameraController;
List<CameraImage> _frameBuffer = [];
bool _isRecordingPreview = false;

void startSpeedPreview(double speed) {
  _cameraController.startImageStream((CameraImage image) {
    if (_isRecordingPreview) {
      _frameBuffer.add(image);
      // Calculate frame interval based on speed
      int interval = (1 / speed).round();
      if (_frameBuffer.length >= interval) {
        // Render the first frame in the buffer
        _renderFrame(_frameBuffer.first);
        _frameBuffer.removeRange(0, interval);
      }
    }
  });
}

void _renderFrame(CameraImage image) {
  // Convert CameraImage to a widget (use packages like image or image_picker for conversion)
  // Update your preview UI with this frame
}

Notes:

  • This is a simplified approach—you'll need to handle image conversion efficiently to avoid lag.
  • Performance will vary by device; test on target devices to ensure smoothness.

内容的提问来源于stack exchange,提问作者user13135379

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:22:44