gTTS /python/ - can you text message the .mp3 file in your script? - python-3.x

twilio api says it only handles mp4 for audio. Anyone have a solution to save python text to speach as .mp3 or .mp4 and text said audio file or a call and the mp3 audio plays.

Twilio developer evangelist here.
Twilio does support mp3 files for audio, as well as wav, aiff, gsm and ulaw.
It looks as though that's exactly what gTTS does for you. The example in the documentation is:
>>> from gtts import gTTS
>>> tts = gTTS('hello')
>>> tts.save('hello.mp3')
Alternatively, you could just use Twilio's text to speech as part of your call, with <Say>. If you are not satisfied with the basic voices you can now use voices from AWS Polly in Twilio Voice.
Let me know if that helps at all.

Related

PyTube downloads audio file in mp4 format. How to fix?

Good Afternoon. I'm beginner in python. So, I was trying to make YouTube Video/Audio Downloader with PyTube (For Educational Purpose only). I have seen many videos on youtube and I was trying to make this tool better. So I have added video/audio choosing option and resulation/quality choosing option. The Good thing is I have successfully made Video downloader. But I got a problem in the Audio Downloader. The problem is, PyTube downloads the audio file in MP4 format. I have searched on google and youtube. But I could not find any solution. I want to rename the from mp4 to mp3 (cause the file is OK, but the format is wrong). as a beginner, I don't know how to save a downloading file in somewhere else (temporary folder), and rename it then transfer it to output folder. I tried to add filename=link.title+'mp3'. But It returns this error: OSError: [Errno 22] Invalid argument: 'G:/Downloaded Videos/Latest English Ringtone | Turkish Bgm Ringtone 2021 | Bad Boy | Attitude Tone | Villain Ringtone.mp3'
here is my Code:
from pytube import YouTube
link='https://www.youtube.com/watch?v=KrhPrPK2owA'
link=YouTube(link)
print('Title:',link.title+'\n'+'Views:',link.views)
streams=link.streams.filter(type='audio')
kbps_list=[]
itag_list=[]
print('Available Kbps: ',end='')
for s in streams:
i=s.itag
s=s.abr
if s not in kbps_list:
kbps_list.append(s)
itag_list.append(i)
print(s,end=' ')
reso=input('\nEnter Kbps to download: ')
if reso not in kbps_list:
print('This Kbps is not available')
from sys import exit
exit()
reso=kbps_list.index(reso)
final=streams.get_by_itag(itag_list[reso])
print('Downloading...')
final.download('G:/Downloaded Videos/')
# final.download('G:/Downloaded Videos/', filename=link.title+'mp3')======================
# if I add custom filename, It returns the error ========================================
print('Successfully Downloaded!')
Need to set the file name and can download in mp3 as you tried but it will still be in the mp4a codec. Not sure if that matters to you.
Your title is all weird and it includes the file path. Probably why link.title is not working. Try the below code to strip the title and the file path.
import os
head tail = os.path.split(link.title)
final=streams.get_by_itag(itag_list[reso]).download(filename=tail.strip(" | ") + ".mp3")
You can also set output directory by passing the argument output_path="some location".
It would look like:
final=streams.get_by_itag(itag_list[reso]).download(output_path="some location", filename=tail.strip(" | ") + ".mp3")

Can moviepy handle aiff audio?

I have a python script that pulls random audio clips from a hard drive. It finds wav and mp3 files and moviepy deals with them fine. When I added the aiff extensions to the search query I get this from moviepy...
codec = extensions_dict[ext[1:]]['codec'][0] KeyError: "'aiff'"
Does anyone know if moviepy specifically does not handle aiff audio?
Could there be a simple way to add aiff handling to moviepy?
Thanks for the head's up Tom. In case this can help someone else, here's what is working for me now.
for clip, title in audioClips.items(): #Iterate by key & value through the dictionary
#print(f"The title is {title}.")
if title.endswith('.aif') or title.endswith('aiff'):
clip.write_audiofile(dirpath+str(title),codec='pcm_s16le')
else:
clip.write_audiofile(dirpath+str(title))
clip.close()

SIM800C : Uploaded audio AMR file has noise when played during call

I am able to upload the AMR file to SIM800C successfully.
When I play the uploaded audio file during the call using the below command :
#if CALL_RECORDED_AUDIO
Serial1.print("AT+CMEDPLAY=1,C:\\REC\\");
// "Command Media PLAY" -> to play an audio if it is a recorded audio
#else
Serial1.print("AT+CMEDPLAY=1,C:\\User\\");
// "Command Media PLAY" -> to play an audio if it is a uploaded audio
#endif
Played audio always has noise, from C:\User\.
However if I record the audio during call and save it. Play the recorded audio during next call then there is no noise. ( By defining CALL_RECORDED_AUDIO in above code snippet)
according to the documentation of the sim800 it is necessary to play a sound wav during the call
Note
. mode 2 and 3 are not supported when playing audio file during call.
. The audio file can not be played duirng incoming call or outgoing call.
. Only support WAV, PCM, AMR and MP3 format.
. Only support WAV format with 8K 16bit during call.
page 201/202 of the sim800 guide
personally i did not suck having no sim800
I think the recording of a call must be in .WAV format
let me know if it works

Can google speech API convert text to speech?

I used Google speech API ti successfully convert speech to text using following code.
import speech_recognition as sr
import os
#obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# recognize speech using Google Cloud Speech
GOOGLE_CLOUD_SPEECH_CREDENTIALS = r"""{KEY}
"""
# INSERT THE CONTENTS OF THE GOOGLE CLOUD SPEECH JSON CREDENTIALS FILE HERE
try:
speechOutput = (r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS, language="si-LK"))
except sr.UnknownValueError:
speechOutput = ("Google Cloud Speech could not understand audio")
except sr.RequestError as e:
speechOutput = ("Could not request results from Google Cloud Speech service; {0}".format(e))
print(speechOutput)
I want to know if i can convert text to speech using the same API? If not what API to use and the sample python code for that.
Thank you!
For this you'll need to use the new Text-to-Speech API which is in Beta as of now. You can find a Python quickstart in the Client Library section of the docs. The sample is part of the python-docs-sample repo. Adding the relevant part of the example here for better visibility:
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
Update: rate and pitch configuration
You can enclose the text elements in a <prosody> tag to modify the rateand pitch. For example:
<prosody rate="slow" pitch="-2st">Can you hear me now?</prosody>
The possible values for those follow the W3 specifications which can be found here. The SSML docs for Text-to-Speech API detail this and they also provide some samples.
Also, you can control the general audio playback rate with the speed option in <audio>, which currently accepts values from 50 to 200% (in 1% increments).

Convert any audio file to mp3 with python

I want to convert any audio file (flac, wav,...) to mp3 with python
I am a noob , I tried pydub but I didn't found out how to make ffmpeg work with it, and If I'm right it can't convert flac file.
The idea of my project is to :
Make musicBee send the path of the 'now playing' track (by pressing the assigned shortcut) to my python file which would convert the music if it is not in mp3 and send it to a folder. (Everything in background so I don't have to leave what I'm doing to make the operation)
You can use the following the code:
from pydub import AudioSegment
wav_audio = AudioSegment.from_file("audio.wav", format="wav")
raw_audio = AudioSegment.from_file("audio.wav", format="raw",
frame_rate=44100, channels=2, sample_width=2)
wav_audio.export("audio1.mp3", format="mp3")
raw_audio.export("audio2.mp3", format="mp3")
You can also look here for more options.
flac_audio = AudioSegment.from_file("sample.flac", "flac")
flac_audio.export("sampleMp3.mp3", format="mp3")

Resources