इस ट्यूटोरियल में, हम ऑडियो फ़ाइलों के साथ काम करेंगे। हम ऑडियो को पहचानने के लिए चंक्स में विभाजित करेंगे। हम ऑडियो फ़ाइलों की सामग्री को टेक्स्ट फ़ाइलों में भी स्टोर करेंगे। निम्न मॉड्यूल को नीचे दिए गए कमांड का उपयोग करके इंस्टॉल करें।
pip install pydub
यदि आप उपरोक्त कमांड चलाते हैं, तो आपको निम्न सफल संदेश मिलेगा।
Collecting pydub Downloading https://files.pythonhosted.org/packages/79/db/eaf620b73a1eec3c8c6f8f5b0b236a50f9da88ad57802154b7ba7664d0b8/pydub-0.23.1-py2.py3-none-any.whl Installing collected packages: pydub Successfully installed pydub-0.23.1
pip install audioread
यदि आप उपरोक्त कमांड चलाते हैं, तो आपको निम्न सफल संदेश मिलेगा।
Collecting audioread Downloading https://files.pythonhosted.org/packages/2e/0b/940ea7861e0e9049f09dcfd72a90c9ae55f697c17c299a323f0148f913d2/audioread-2.1.8.tar.gz Building wheels for collected packages: audioread Building wheel for audioread (setup.py): started Building wheel for audioread (setup.py): finished with status 'done' Created wheel for audioread: filename=audioread-2.1.8-cp37-none-any.whl size=23098 sha256=92b6f46d6b4726e7a13233dc9d84744ba74e23187123e67f663650f24390dc9d Stored in directory: C:\Users\hafeezulkareem\AppData\Local\pip\Cache\wheels\b9\64\09\0b6417df9d8ba8bc61a7d2553c5cebd714ec169644c88fc012 Successfully built audioread Installing collected packages: audioread Successfully installed audioread-2.1.8
pip install SpeechRecognition
यदि आप उपरोक्त कमांड चलाते हैं, तो आपको निम्न सफल संदेश मिलेगा।
Collecting SpeechRecognition Downloading https://files.pythonhosted.org/packages/26/e1/7f5678cd94ec1234269d23756dbdaa4c8cfaed973412f88ae8adf7893a50/SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8MB) Installing collected packages: SpeechRecognition Successfully installed SpeechRecognition-3.8.1
प्रक्रिया में हमारे दो चरण हैं।
ऑडियो को चंक्स में विभाजित करना।
SpeechRecognition का उपयोग करके सामग्री निकालना।
अपनी लाइब्रेरी से एक ऑडियो फ़ाइल लें। आइए कोड शुरू करें।
उदाहरण
# मॉड्यूल आयात करें
import pydub
import speech_recognition
# ऑडियो फ़ाइल प्राप्त करें
audio = pydub.AudioSegment.from_wav('audio.wav')
# मिलीसेकंड में ऑडियो की लंबाई
audio_length = len(audio)
print(f'Audio Length: {audio_length}')
# चंक काउंटर
chunk_counter = 1
audio_text = open('audio_text.txt', 'w+')
# ऑडियो को स्लाइस करने का बिंदु सेट करें
point = 60000
# ओवरलैप - स्लाइसिंग के बाद शेष ऑडियो
rem = 8000
# चंक्स और समाप्ति फ्लैग को ट्रैक करने के लिए वेरिएबल इनिशियलाइज़ करें
flag = 0
start = 0
end = 0
# rem के इंक्रीमेंट के साथ ऑडियो के माध्यम से पुनरावृति
for i in range(0, 2 * audio_length, point):
# पहली पुनरावृति में end = rem
if i == 0:
start = 0
end = point
else:
# अन्य पुनरावृत्तियाँ
start = end - rem
end = start + point
# यदि end audio_length से अधिक है
if end >= audio_length:
end = audio_length
# रुकने का संकेत
flag = 1
# ऑडियो से एक चंक प्राप्त करें
chunk = audio[start:end]
# चंक नाम
chunk_name = f'chunk_{chunk_counter}'
# चंक को स्थानीय स्टोरेज में स्टोर करें
chunk.export(chunk_name, format='wav')
# चंक प्रिंट करें
print(f'{chunk_name} start: {start} end: {end}')
# चंक काउंटर बढ़ाएं
chunk_counter += 1
# ऑडियो से टेक्स्ट पहचानें
# रिकॉग्नाइज़र इनिशियलाइज़ करें
recognizer = speech_recognition.Recognizer()
# सुना गया ऑडियो बनाएं
with speech_recognition.AudioFile(chunk_name) as chunk_audio:
chunk_listened = recognizer.listen(chunk_audio)
# ऑडियो से सामग्री पहचानें
try:
# चंक से सामग्री प्राप्त करें
content = recognizer.recognize_google(chunk_listened)
# फ़ाइल में लिखें
audio_text.write(content + '\n')
# यदि पहचाना नहीं गया
except speech_recognition.UnknownValueError:
print('Audio is not recognized')
# इंटरनेट त्रुटि
except speech_recognition.RequestError as Error:
print("Can't connect to the internet")
# फ्लैग जांचें
if flag == 1:
audio_text.close()
breakआउटपुट
यदि आप उपरोक्त कोड चलाते हैं, तो आपको निम्न परिणाम मिलेंगे।
Audio Length: 480052 chunk_1 start: 0 end: 60000 chunk_2 start: 52000 end: 112000 chunk_3 start: 104000 end: 164000 chunk_4 start: 156000 end: 216000 chunk_5 start: 208000 end: 268000 chunk_6 start: 260000 end: 320000 chunk_7 start: 312000 end: 372000 chunk_8 start: 364000 end: 424000 chunk_9 start: 416000 end: 476000 chunk_10 start: 468000 end: 480052
फ़ाइल सामग्री की जांच।
# फ़ाइल को रीड मोड में खोलें
with open('audio_text.txt', 'r') as file:
print(file.read())यदि आप उपरोक्त कोड चलाते हैं, तो आपको निम्न परिणाम मिलेगा।
English and I am here in San Francisco I am back in San Francisco last week we were in Texas at a teaching country and The Reader of the teaching conference was a plan e Re improve teaching as a result you are house backup file with bad it had some English is coming soon one day only time 12 o1 a.m. everything about her English now or powering on my email list sports in your city check your email email Harjeet girlfriend next Tuesday checking the year enjoying office English keep listening keep smiling keep enjoying your English learning
निष्कर्ष
यदि ट्यूटोरियल के संबंध में आपके कोई संदेह हैं, तो उन्हें कमेंट सेक्शन में उल्लेख करें।