fastperson / utils.py
kwmr's picture
upload
9504de3
raw
history blame
1.81 kB
import os
import json
import base64
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from pytube import YouTube
from scipy.signal import resample
db = firestore.client()
# 環境変数から秘密鍵を取得
encoded_key = os.environ["FIREBASE_CREDENTIALS_BASE64"]
# Base64エンコードされた秘密鍵をデコード
decoded_key = base64.b64decode(encoded_key)
# デコードされた秘密鍵を使ってCredentialオブジェクトを作成
cred = credentials.Certificate(json.loads(decoded_key))
# Firebase Admin SDKを初期化
firebase_admin.initialize_app(cred)
def log_firestore(user_id="000000", message="test"):
doc_ref = db.collection("button_clicks").document()
doc_ref.set({
"user_id": user_id,
"message": message,
"timestamp": firestore.SERVER_TIMESTAMP
})
def get_youtube(user_id, video_url):
# YouTubeの動画をダウンロード
log_firestore(user_id=user_id, message=f'Download Video:{video_url}')
print("Start download video")
yt = YouTube(video_url)
abs_video_path = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename='download.mp4', output_path='movies/')
print("Success download video")
print(abs_video_path)
return abs_video_path
def two_chnnel_to_one_channel(sample):
# 音声を2チャンネルから1チャンネルに変換
left_channel = sample[:, 0]
right_channel = sample[:, 1]
mono_sample = (left_channel + right_channel) / 2
return mono_sample
def convert_sample_rate(data, original_sr, target_sr):
# 音声データのサンプリング周波数を変更
target_length = int(len(data) * target_sr / original_sr)
return resample(data, target_length)