-
Notifications
You must be signed in to change notification settings - Fork 0
/
speechpy-text-excel_notes.txt
97 lines (82 loc) · 3.81 KB
/
speechpy-text-excel_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
This code performs automatic transcription of audio from video files using the Google Speech Recognition API. Here are the instructions to use the code:
Make sure you have the necessary dependencies installed: os, speech_recognition, moviepy, and pandas. You can install them using pip: pip install os speech_recognition moviepy pandas.
Import the required modules at the beginning of your script:
python
Copy code
import os
import speech_recognition as sr
from moviepy.editor import AudioFileClip
from moviepy.editor import VideoFileClip
import pandas as pd
Initialize the SpeechRecognition recognizer:
python
Copy code
r = sr.Recognizer()
Set the folder path containing the video files:
python
Copy code
folder_path = r"E:\Speechpy\simcomp-2"
Create an empty list to store the results:
python
Copy code
results = []
Create a directory to store the TXT files:
python
Copy code
output_dir = os.path.join(folder_path, "transcriptions")
os.makedirs(output_dir, exist_ok=True)
Iterate through the files in the folder and perform transcription:
python
Copy code
for filename in os.listdir(folder_path):
try:
# Generate the full path of the video file
video_file = os.path.join(folder_path, filename)
# Load the video file using moviepy
video = VideoFileClip(video_file)
# Extract the audio from the video
audio = video.audio
# Save the extracted audio as a temporary file
audio_file = "temp_audio.wav"
audio.write_audiofile(audio_file)
# Open the audio file for speech recognition
with sr.AudioFile(audio_file) as source:
# Load the audio to memory
audio_data = r.record(source)
# Use Google Speech Recognition to transcribe the audio
transcript = r.recognize_google(audio_data)
# Append the filename and transcript to the results list
results.append((filename, transcript))
# Create a TXT file for each transcript
output_file = os.path.join(output_dir, os.path.splitext(filename)[0] + ".txt")
with open(output_file, "w") as f:
f.write(transcript)
# Print the transcript
print(f"Transcript for {filename}: {transcript}")
except sr.UnknownValueError:
print(f"Could not transcribe {filename}")
except sr.RequestError as e:
print(f"Error occurred during transcription for {filename}: {e}")
finally:
# Delete the temporary audio file
if audio:
audio.close()
Check if the lengths of the two arrays are the same and save the results as an Excel file:
python
Copy code
if len(os.listdir(folder_path)) == len(results):
# Create a pandas DataFrame with the results
df = pd.DataFrame(results, columns=['Filename', 'Transcript'])
# Add a column for the file names
df['File Name'] = [os.path.splitext(filename)[0] + ".txt" for filename in df['Filename']]
# Save the DataFrame as an Excel file
output_file = os.path.join(output_dir, "transcripts.xlsx")
try:
df.to_excel(output_file, index=False)
print(f"Transcripts saved to {output_file}")
except Exception as e:
print(f"Error occurred while saving transcripts: {e}")
else:
print("Error: Length mismatch between filenames and transcripts.")
Make sure to replace the folder_path variable with the path to the folder containing your video files. The transcriptions will be saved as TXT files in the "transcriptions" subdirectory within the specified folder, and a summary of the transcriptions will be saved as an Excel file named "transcripts.xlsx" in the same subdirectory.
Note: This code uses the Google Speech Recognition API, so you'll need an internet connection for the transcription to work.