I have an image picker in flutter ro get the video from the device. and then i created a function to extract the audio using ffmpeg_kit_flutter package.
<code> Future<void> _convertVideoToAudio() async {
if (_pickedVideo != null) {
bool? permissionGranted = await _requestStoragePermission();
if (permissionGranted != true) {
print("Storage permission denied.");
return;
}
String videoPath = _pickedVideo!.path;
_outputPath = await getOutputFilePath(); // Get platform-specific path
try {
// Ensure the output directory exists
await Directory(path.dirname(_outputPath)).create(recursive: true);
await FFmpegKit.execute(
"-i $videoPath -vn -c:a libmp3lame -q:a 2 $_outputPath"); // FFmpeg command
print("Video converted to audio successfully!");
_showSuccessDialog(); // Display success dialog
try {
final String fileName = path.basename(_outputPath);
final transcription =
await _sendAudioForTranscription(_outputPath, fileName);
if (transcription != null) {
setState(() {
_transcription = transcription;
});
} else {
setState(() {
_transcription = "Transcription failed";
});
}
} catch (e) {
print('Error in transcription request: $e');
setState(() {
_transcription = "Network request failed";
});
}
} catch (e) {
print("Error converting video: $e");
_showErrorDialog(); // Display error dialog
} finally {
setState(() {
_pickedVideo = null; // Clear selected video
});
}
} else {
print("Please pick a video first.");
}
}
</code>
<code> Future<void> _convertVideoToAudio() async {
if (_pickedVideo != null) {
bool? permissionGranted = await _requestStoragePermission();
if (permissionGranted != true) {
print("Storage permission denied.");
return;
}
String videoPath = _pickedVideo!.path;
_outputPath = await getOutputFilePath(); // Get platform-specific path
try {
// Ensure the output directory exists
await Directory(path.dirname(_outputPath)).create(recursive: true);
await FFmpegKit.execute(
"-i $videoPath -vn -c:a libmp3lame -q:a 2 $_outputPath"); // FFmpeg command
print("Video converted to audio successfully!");
_showSuccessDialog(); // Display success dialog
try {
final String fileName = path.basename(_outputPath);
final transcription =
await _sendAudioForTranscription(_outputPath, fileName);
if (transcription != null) {
setState(() {
_transcription = transcription;
});
} else {
setState(() {
_transcription = "Transcription failed";
});
}
} catch (e) {
print('Error in transcription request: $e');
setState(() {
_transcription = "Network request failed";
});
}
} catch (e) {
print("Error converting video: $e");
_showErrorDialog(); // Display error dialog
} finally {
setState(() {
_pickedVideo = null; // Clear selected video
});
}
} else {
print("Please pick a video first.");
}
}
</code>
Future<void> _convertVideoToAudio() async {
if (_pickedVideo != null) {
bool? permissionGranted = await _requestStoragePermission();
if (permissionGranted != true) {
print("Storage permission denied.");
return;
}
String videoPath = _pickedVideo!.path;
_outputPath = await getOutputFilePath(); // Get platform-specific path
try {
// Ensure the output directory exists
await Directory(path.dirname(_outputPath)).create(recursive: true);
await FFmpegKit.execute(
"-i $videoPath -vn -c:a libmp3lame -q:a 2 $_outputPath"); // FFmpeg command
print("Video converted to audio successfully!");
_showSuccessDialog(); // Display success dialog
try {
final String fileName = path.basename(_outputPath);
final transcription =
await _sendAudioForTranscription(_outputPath, fileName);
if (transcription != null) {
setState(() {
_transcription = transcription;
});
} else {
setState(() {
_transcription = "Transcription failed";
});
}
} catch (e) {
print('Error in transcription request: $e');
setState(() {
_transcription = "Network request failed";
});
}
} catch (e) {
print("Error converting video: $e");
_showErrorDialog(); // Display error dialog
} finally {
setState(() {
_pickedVideo = null; // Clear selected video
});
}
} else {
print("Please pick a video first.");
}
}
and for getting the path i have this function
<code> Future<String> getOutputFilePath() async {
final directory = await getApplicationDocumentsDirectory();
final downloadsDirectory = Directory('${directory.path}/downloads');
if (!(await downloadsDirectory.exists())) {
await downloadsDirectory.create(recursive: true);
}
final String fileName = path
.basename(_pickedVideo!.path)
.replaceAll('.mp4', '.mp3'); // Replace extension
final filePath = '${downloadsDirectory.path}/$fileName';
return filePath;
}
</code>
<code> Future<String> getOutputFilePath() async {
final directory = await getApplicationDocumentsDirectory();
final downloadsDirectory = Directory('${directory.path}/downloads');
if (!(await downloadsDirectory.exists())) {
await downloadsDirectory.create(recursive: true);
}
final String fileName = path
.basename(_pickedVideo!.path)
.replaceAll('.mp4', '.mp3'); // Replace extension
final filePath = '${downloadsDirectory.path}/$fileName';
return filePath;
}
</code>
Future<String> getOutputFilePath() async {
final directory = await getApplicationDocumentsDirectory();
final downloadsDirectory = Directory('${directory.path}/downloads');
if (!(await downloadsDirectory.exists())) {
await downloadsDirectory.create(recursive: true);
}
final String fileName = path
.basename(_pickedVideo!.path)
.replaceAll('.mp4', '.mp3'); // Replace extension
final filePath = '${downloadsDirectory.path}/$fileName';
return filePath;
}
but this is not working somehow. b/c after i get the audio i’m uploading it to a server with http, then it displays that there is no path where the audio supposed to be.
can someone help me with this? any help is appreciated.