
[英]AttributeError: 'NoneType' object has no attribute 'close'
[英]Speech recognition via Sphinx engine produces AttributeError: 'NoneType' object has no attribute 'close'
我正在尝试使用以下代码转录视频,但收到以下错误:
ERROR : transcribe_video_to_text
audio.close()
AttributeError: 'NoneType' object has no attribute 'close'
您能否分享您解决这个问题的想法,或者建议可以执行转录的替代库或 AI?
代码:
import moviepy.editor as mp
import speech_recognition as sr
def transcribe_video_to_text(video_path):
# Extract audio from the video
video = mp.VideoFileClip(video_path)
audio = video.audio.write_audiofile("temp_audio.wav")
# Initialize the speech recognition object
recognizer = sr.Recognizer()
# Read the audio file
with sr.AudioFile("temp_audio.wav") as source:
audio_data = recognizer.record(source)
# Perform speech recognition on the audio
text = recognizer.recognize_sphinx(audio_data, keyword_entries=[('whisper', 1.0)])
# Remove the temporary audio file
video.close()
audio.close()
os.remove("temp_audio.wav")
return text
# Provide the path to your video file
video_path = "Gods Architect Antoni Gaudis glorious vision.mp4"
# Transcribe the video to text
transcription = transcribe_video_to_text(video_path)
# Print the resulting transcription
print("Video transcription:")
print(transcription)
您收到错误是因为对write_audiofile()
的调用返回None
,而您正试图对其调用close()
。
您不需要首先将返回值存储在audio
中。 下面更新的代码将转录您的视频:
import os
import moviepy.editor as mp
import speech_recognition as sr
def transcribe_video_to_text(video_path):
# Extract audio from the video
video = mp.VideoFileClip(video_path)
video.audio.write_audiofile("temp_audio.wav")
# Initialize the speech recognition object
recognizer = sr.Recognizer()
# Read the audio file
with sr.AudioFile("temp_audio.wav") as source:
audio_data = recognizer.record(source)
# Perform speech recognition on the audio
text = recognizer.recognize_sphinx(audio_data)
video.close()
# Remove the temporary audio file
os.remove("temp_audio.wav")
return text
# Provide the path to your video file
video_path = "Gods Architect Antoni Gaudis glorious vision.mp4"
# Transcribe the video to text
transcription = transcribe_video_to_text(video_path)
# Print the resulting transcription
print("Video transcription:")
print(transcription)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.