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

Flutter视频录制获.temp文件而非MP4,无法保存至相册及API传输

视频录制APP问题解决指南

问题现象

  • 停止录制后获取的视频文件后缀为.temp,而非预期的.MP4
  • 调用Gal.putVideo保存视频到相册时抛出GalException/UNEXPECTED错误
  • 需要实现视频通过API上传的功能

当前代码

void initState() {
  print('Inside initState');
  startCamera();
  super.initState();
}

void startCamera() async {
  print('Inside startCamera');
  cameraController = CameraController(cameras[1], ResolutionPreset.medium);
  cameraValue = cameraController.initialize();
  await Future.delayed(Duration(seconds: 2));
  startRecording();
}

void startRecording() async {
  print('Inside startRecording');

  try {
    print('Inside startRecording Try block');

    // 开始录制视频
    await cameraValue;
    await cameraController.prepareForVideoRecording();

    await cameraController.startVideoRecording();
  } catch (e) {
    print("异常信息:" + e.toString());
    Get.snackbar('错误', e.toString());
  }
}

void stopRecording() async {
  print('Inside stop recording');
  XFile video = await cameraController.stopVideoRecording();

  print('录制停止,路径:' + video.path);

  Get.snackbar('视频路径', video.path);

  try {
    await Gal.putVideo(video.path);
  } catch (e) {
    print("相册保存异常:" + e.toString());
  }
}

调试输出

I/flutter (11116): Inside initState
I/flutter (11116): Inside startCamera
I/flutter (11116): Inside startRecording
I/flutter (11116): Inside startRecording Try block
I/flutter (11116): Inside stop recording
I/flutter (11116): Inside Recording stopped /data/user/0/com.example.design4you/cache/REC7110041240717347649.temp
I/flutter (11116): inside [GalException/UNEXPECTED]: An unexpected error has occurred.

解决方案

1. 修正文件后缀为MP4

相机控制器返回的是临时文件,需手动复制并重命名为标准MP4格式:

import 'package:path_provider/path_provider.dart';
import 'dart:io';

void stopRecording() async {
  print('Inside stop recording');
  XFile tempVideo = await cameraController.stopVideoRecording();

  // 创建目标MP4文件路径
  final appDir = await getApplicationDocumentsDirectory();
  final timestamp = DateTime.now().millisecondsSinceEpoch;
  final mp4Path = '${appDir.path}/REC$timestamp.mp4';
  // 复制临时文件到目标路径
  final mp4File = await tempVideo.copy(mp4Path);

  print('录制停止,MP4路径:' + mp4File.path);
  Get.snackbar('视频路径', mp4File.path);

  // 后续操作使用mp4File
}

需在pubspec.yaml添加依赖:

dependencies:
  path_provider: ^2.1.2

2. 修复相册保存失败问题

使用重命名后的MP4文件即可解决系统不识别的问题:

try {
  await Gal.putVideo(mp4File.path);
  print('视频已保存到相册');
} catch (e) {
  print("相册保存错误:" + e.toString());
}

3. 实现视频API上传

使用http包发送Multipart请求上传视频:

import 'package:http/http.dart' as http;

Future<void> uploadVideo(String videoPath) async {
  final uploadUrl = Uri.parse('你的API上传地址');
  final request = http.MultipartRequest('POST', uploadUrl);

  // 添加视频文件
  request.files.add(await http.MultipartFile.fromPath('video', videoPath));
  // 可添加额外表单参数
  // request.fields['title'] = '录制视频';

  final response = await request.send();
  if (response.statusCode == 200) {
    print('视频上传成功');
  } else {
    print('上传失败,状态码:${response.statusCode}');
  }
}

添加http依赖:

dependencies:
  http: ^1.2.1

4. 优化相机初始化逻辑

移除不可靠的Future.delayed,直接等待相机初始化完成:

void startCamera() async {
  print('Inside startCamera');
  cameraController = CameraController(cameras[1], ResolutionPreset.medium);
  // 等待相机初始化完成
  await cameraController.initialize();
  startRecording();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 01:04:55