$screenshot, "timestamp" => floor(sizeof($array) * $framesInterval), ]; } return $array; } /** * Extract audio from the video file. * Using ffmpeg to extract audio from the video file. * The audio will be saved in a temporary directory as an MP3 file. * If the audio extraction fails, it will return null. * @param string $filePath * @return string|null */ protected function extractAudioFromVideo(string $filePath): ?string { $tempDir = sys_get_temp_dir() . '/video_audio'; if (!is_dir($tempDir)) { mkdir($tempDir, 0777, true); } else { // Clear the directory if it already exists array_map('unlink', glob($tempDir . '/*')); } $outputFile = $tempDir . '/audio.mp3'; $command = "ffmpeg -i " . escapeshellarg($filePath) . " " . escapeshellarg($outputFile); exec($command); if (file_exists($outputFile)) { return $outputFile; } return null; } }