How to export a video with a widget overlay in a Flutter app?

I’m developing a Flutter app for a caption embeding on a video that needs to export a video file after processing it. I’m using the flutter_ffmpeg_kit package. However, I’m having trouble getting the export to work correctly.

Here’s the code I’m using:

initially this is my stack

                  Expanded(
                    child: Stack(
                      children: [
                        Center(
                          child: _videoPlayerController.value.isInitialized
                              ? AspectRatio(
                                  aspectRatio:
                                      _videoPlayerController.value.aspectRatio,
                                  child: VideoPlayer(_videoPlayerController),
                                )
                              : CircularProgressIndicator(),
                        ),
                        if (_currentCaption.isNotEmpty)
                          Positioned.fill(
                            child: Center(child: _buildCaptionText()),
                          ),
                      ],
                    ),
                  ),

and in export button i executed this function

 Future<void> _exportVideo() async {
    setState(() {
      _isProcessing = true;
    });

    try {
      final directory = await getExternalStorageDirectory();
      final rootPath = directory?.parent.parent.parent.parent.path;
      final mobixPath = path.join(rootPath!, 'Mobix App');
      final appPath = path.join(mobixPath, 'Caption');
      final outputPath = path.join(appPath, 'Output');

      // Create the directories if they don't exist
      await Directory(outputPath).create(recursive: true);

      final timestamp = DateTime.now().millisecondsSinceEpoch;
      final outputFilePath = path.join(outputPath, 'output-$timestamp.mp4');


      // Generate the FFmpeg command
      final ffmpegCommand = _generateFFmpegCommand(
        widget.videoPath,
        outputFilePath,
        widget.words,
        _fontSize,
        _isBold,
        _isItalic,
        _fontColor,
        _backgroundColor,
      );

      // Execute the FFmpeg command
      await FFmpegKit.execute(
        ffmpegCommand,
      ).then(
        (session) async {
          // Update progress if needed
          final returnCode = await session.getReturnCode();
          if (ReturnCode.isSuccess(returnCode)) {
            setState(() {
              _outputFilePath = outputFilePath;
            });
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Export successful: $_outputFilePath')),
            );
          } else {
            print('Export failed with rc: $returnCode');

            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Export failed with rc: $returnCode')),
            );
          }
          setState(() {
            _isProcessing = false;
          });
        },
      );
    } catch (e) {
      print('Export failed: $e');
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Export failed: $e')),
      );
      setState(() {
        _isProcessing = false;
      });
    }
  }

  String _generateFFmpegCommand(
    String inputPath,
    String outputPath,
    List<dynamic> words,
    double fontSize,
    bool isBold,
    bool isItalic,
    Color fontColor,
    Color backgroundColor,
  ) {
    final ffmpegCommand = StringBuffer();

    // Add input file
    ffmpegCommand.write('-i $inputPath ');

    // Add subtitles filter
    final subtitleFilter = StringBuffer();
    for (var word in words) {
      final startTime = word['startTime'].toDouble();
      final endTime = word['endTime'].toDouble();
      final caption = word['word'];

      final fontStyle = isBold && isItalic
          ? 'bold italic'
          : isBold
              ? 'bold'
              : isItalic
                  ? 'italic'
                  : 'normal';
      final fontColorHex = fontColor.value.toRadixString(16).substring(2);
      final backgroundColorHex =
          backgroundColor.value.toRadixString(16).substring(2);

      subtitleFilter.write(
          "drawtext=text='$caption':x=(w-tw)/2:y=h-(2*lh):fontcolor=$fontColorHex:fontsize=$fontSize:fontStyle=$fontStyle:box=1:[email protected]:boxborderw=5:enable='between(t,$startTime,$endTime)',");
    }
    ffmpegCommand.write('-vf "${subtitleFilter.toString()}" ');

    // Add output file
    ffmpegCommand.write('$outputPath');

    return ffmpegCommand.toString();
  }

when i run this it returns ReturnCode 1. what am i doing wrong?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật