Created
November 16, 2025 10:25
-
-
Save farzadhallaji/0be752a3785c293a641c5d328a4cf168 to your computer and use it in GitHub Desktop.
voice to text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import sys | |
| import glob | |
| import whisper | |
| def find_audio_files(directory): | |
| # mp3 and m4a, case-insensitive | |
| patterns = ["*.mp3", "*.MP3", "*.m4a", "*.M4A"] | |
| files = [] | |
| for pattern in patterns: | |
| files.extend(glob.glob(os.path.join(directory, pattern))) | |
| return sorted(files) | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("Usage: python batch_transcribe.py /path/to/dir [model_name]") | |
| sys.exit(1) | |
| audio_dir = sys.argv[1] | |
| model_name = sys.argv[2] if len(sys.argv) >= 3 else "small" # you can change to "medium" | |
| if not os.path.isdir(audio_dir): | |
| print(f"Error: '{audio_dir}' is not a directory.") | |
| sys.exit(1) | |
| print(f"Loading Whisper model: {model_name}") | |
| model = whisper.load_model(model_name) | |
| audio_files = find_audio_files(audio_dir) | |
| if not audio_files: | |
| print("No .mp3 or .m4a files found in that directory.") | |
| sys.exit(0) | |
| for path in audio_files: | |
| base, _ = os.path.splitext(path) | |
| txt_path = base + ".txt" | |
| if os.path.exists(txt_path): | |
| print(f"Skipping (txt already exists): {os.path.basename(path)}") | |
| continue | |
| print(f"Transcribing: {os.path.basename(path)}") | |
| try: | |
| result = model.transcribe(path) | |
| text = result.get("text", "").strip() | |
| with open(txt_path, "w", encoding="utf-8") as f: | |
| f.write(text + "\n") | |
| print(f"Saved: {os.path.basename(txt_path)}") | |
| except Exception as e: | |
| print(f"Failed on {os.path.basename(path)}: {e}") | |
| if __name__ == "__main__": | |
| main() | |
| # python batch_transcribe.py /path/to/your/audio medium |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment