I’m building a Flutter app that allows users to upload a video file and generate a text transcript of the audio. I’ve implemented the code to pick a video file from the gallery and display it using the video_player package. However, when I try to start the transcription using the speech_to_text package, it starts recording the user’s audio input instead of transcribing the audio from the video file.
Here’s the relevant code I’m using:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import 'package:image_picker/image_picker.dart';
class VideoTranscriptionApp extends StatefulWidget {
@override
_VideoTranscriptionAppState createState() => _VideoTranscriptionAppState();
}
class _VideoTranscriptionAppState extends State<VideoTranscriptionApp> {
VideoPlayerController? _videoPlayerController;
stt.SpeechToText _speechToText = stt.SpeechToText();
List<Map<String, dynamic>> _transcriptionData = [];
void _initializeSpeechToText() {
_speechToText.initialize();
}
Future<void> _pickVideoAndTranscribe() async {
final pickedFile = await ImagePicker().pickVideo(
source: ImageSource.gallery,
);
if (pickedFile != null) {
final videoFile = File(pickedFile.path);
_videoPlayerController = VideoPlayerController.file(videoFile)
..initialize().then((_) {
setState(() {});
_startTranscription();
});
}
}
void _startTranscription() async {
if (await _speechToText.isAvailable) {
_speechToText.listen(
onResult: (result) {
final word = result.recognizedWords;
final start = result.alternates.first.recognizedWords;
final end = result.alternates.last.recognizedWords;
_transcriptionData.add({
'word': word,
'start': start,
'end': end,
});
setState(() {});
},
);
}
}
void _stopTranscription() {
_speechToText.stop();
}
// Rest of the code...
}
The issue I’m facing is that the speech_to_text package is transcribing the user’s audio input instead of the audio from the video file. How can I modify the code to transcribe the audio from the video file?
I’d appreciate any guidance or suggestions on how to achieve this functionality in a Flutter app.