Skip to content

Instantly share code, notes, and snippets.

def speech_to_text(bucket_name, audio_blob_name):
client = speech_v1p1beta1.SpeechClient()
# storage_uri = 'gs://cloud-samples-data/speech/brooklyn_bridge.mp3'
storage_uri = 'gs://' + bucket_name + '/' + audio_blob_name
# The language of the supplied audio
language_code = "en-GB"
# Sample rate in Hertz of the audio data sent
sample_rate_hertz = 44100
encoding = enums.RecognitionConfig.AudioEncoding.MP3
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
def translate(text,language):
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, bytes):
text = text.decode('utf-8')
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(
@armindocachada
armindocachada / google_text_to_speech.py
Created July 21, 2020 15:32
Calling the Google Text To Speech API
def text_to_speech(speak, languageCode, outputFilePath, speed=1.0):
"""Synthesizes speech from the input string of text or ssml.
Note: ssml must be well-formed according to:
https://www.w3.org/TR/speech-synthesis/
"""
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
@armindocachada
armindocachada / ffmpeg_merge_video_with_audio.py
Created July 21, 2020 15:42
Replaces the audio for a given video file videoFilePath with the audio provided by audioFilePath
def merge_video_with_audio_ffmpeg(videoFilePath,audioFilePath,filePathOutput,start_time_audio="00:00:05"):
subprocess.call(['ffmpeg', '-i', videoFilePath,
'-itsoffset', start_time_audio,
'-i', audioFilePath,
'-c:v', 'copy',
'-map', '0:v:0',
'-map', '1:a:0',
filePathOutput, '-y'])
def determineWeather():
"""
We call the weather API and get the weather for the next few hours.
The API itself gives way too much information. All I want to know,
what is the minimum and maximum temperature and whether is going
to rain or snow. And whether it is going to be light or heavy.
>>> determineWeather()
{'precipitation': False, 'heavyRain': False, 'heavySnow': False, 'temperatureCode': 'Hot'}
"""
import http.client
@armindocachada
armindocachada / setupWeatherFlowYeelight.py
Last active August 9, 2020 11:08
Method showing how you can create a lightflow for the Yeelight smart lights using Python
def setupWeatherFlow(bulb,weather,durationFlowSeconds=60):
"""
We use HSV color transitions. In case there is no precipitation, the color will remain static for a minute or two.
If there is precipitation, the light will pulse with varying levels of brightness.
Depending on the temperature the light will be deep red if it is hot or deep blue if it is freezing.
The flow itself will end automatically based on the durationFlowSeconds parameter.
"""
hue = 0
saturation = 100
if weather['temperatureCode'] == "Hot":
@armindocachada
armindocachada / Dockerfile_yeelightweather
Last active August 15, 2020 09:12
Docker files for Yeelight weather
FROM python:rc-slim-buster
RUN TZ=Europe/London && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get -y update && apt-get -y install cron
RUN pip3 install yeelight
COPY files/* /home/
#RUN chmod 755 /script.sh /entry.sh
RUN /usr/bin/crontab /home/crontab.txt
# Run the command on container startup
@armindocachada
armindocachada / .env_template_yeelightweather
Created August 15, 2020 09:21
Yeelight Weather .env file
latitude=<YOUR LATITUDE>
longitude=<YOUR LONGITUDE>
metoffice_client_id=<CLIENT ID>
metoffice_client_secret=<CLIENT SECRET ID>
@armindocachada
armindocachada / nginx-ingress-internal-gke.yaml
Last active January 30, 2021 18:54
Yaml to create a second nginx ingress controller in a separate namespace
apiVersion: v1
kind: Namespace
metadata:
name: ingress-nginx-internal
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
---