Update app.py
Browse files103 unmodified lines10410510610710810911011111211311411511611723 unmodified lines1411421431441451463 unmodified lines150151152153154155156157158159160161162163164165166103 unmodified lineslogger.error(f"Error getting file info: {str(e)}")return Nonedef download_file(url):local_filename = url.split('/')[-1]with requests.get(url, stream=True) as r:r.raise_for_status()with open(local_filename, 'wb') as f:for chunk in r.iter_content(chunk_size=8192):f.write(chunk)return local_filenamedef process_media(file_path, is_url=False):global generated_file, transcription_text23 unmodified linesif not file_info:return "Unable to process file: Could not determine file type", False# Determine if it's audio or videois_audio = any(stream['codec_type'] == 'audio' for stream in file_info['streams'])is_video = any(stream['codec_type'] == 'video' for stream in file_info['streams'])3 unmodified linestry:if is_video:# Extract audio from videosubprocess.run(['ffmpeg', '-i', temp_file, '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', wav_path], check=True)elif is_audio:# Convert audio to WAVsubprocess.run(['ffmpeg', '-i', temp_file, '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', wav_path], check=True)else:return "Unsupported file type: Neither audio nor video detected", Falselogger.info(f"Audio extracted to WAV: {wav_path}")except subprocess.CalledProcessError as e:logger.error(f"FFmpeg conversion failed: {str(e)}")return f"FFmpeg conversion failed: {str(e)}", False# Chunk the audio fileaudio = AudioSegment.from_wav(wav_path)103 unmodified lines10410510610710810911011111211311411511611723 unmodified lines1411421431441451461471483 unmodified lines152153154155156157158159160161162163164165166167168169170171172103 unmodified lineslogger.error(f"Error getting file info: {str(e)}")return Nonedef get_file_info(file_path):try:result = subprocess.run(['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', file_path],capture_output=True, text=True, check=True)return json.loads(result.stdout)except subprocess.CalledProcessError as e:logger.error(f"Error getting file info: {str(e)}")return Nonedef process_media(file_path, is_url=False):global generated_file, transcription_text23 unmodified linesif not file_info:return "Unable to process file: Could not determine file type", Falselogger.info(f"File info: {json.dumps(file_info, indent=2)}")# Determine if it's audio or videois_audio = any(stream['codec_type'] == 'audio' for stream in file_info['streams'])is_video = any(stream['codec_type'] == 'video' for stream in file_info['streams'])3 unmodified linestry:if is_video:# Extract audio from videocmd = ['ffmpeg', '-i', temp_file, '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', wav_path, '-v', 'verbose']elif is_audio:# Convert audio to WAVcmd = ['ffmpeg', '-i', temp_file, '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', wav_path, '-v', 'verbose']else:return "Unsupported file type: Neither audio nor video detected", Falseresult = subprocess.run(cmd, check=True, capture_output=True, text=True)logger.info(f"FFmpeg command output: {result.stdout}")logger.info(f"Audio extracted to WAV: {wav_path}")except subprocess.CalledProcessError as e:logger.error(f"FFmpeg conversion failed. Error output: {e.stderr}")logger.error(f"FFmpeg command: {e.cmd}")logger.error(f"Return code: {e.returncode}")return f"FFmpeg conversion failed: {e.stderr}", False# Chunk the audio fileaudio = AudioSegment.from_wav(wav_path)
app.py+18-12