funapi / routes /ytApi /getPreview.py
imperialwool's picture
Update routes/ytApi/getPreview.py
78eef42
raw
history blame
2.1 kB
import os
import ffmpeg
from .get import *
from .. import helpers
def getPreview(request):
answer = get(request, "previews")
try:
if answer['status'] == "error": return answer
if answer['error']: return answer
except KeyError: pass
except Exception as e: return {"status": "error", "details": { "error_code": 123, "error_details": e }}
urlcode = answer['urlcode']
bitrate = answer['bitrate']
quality = answer['quality']
error_code = answer['ytdlp-code']
try:
if request.method == 'POST': duration = int(request.form['duration'])
else: duration = int(request.args['duration'])
if duration >= 60: duration = 60
except Exception as e:
print(e)
duration = 30
try:
if request.method == 'POST': extension = request.form['extension']
else: extension = request.args['extension']
except: extension = "ogg"
config = helpers.configFile()
if answer['done-or-not']:
return {"status": "pass", "details": {"code": error_code, "name":f"{urlcode}.{extension}", "result": f"{config['url']}/static/previews/{urlcode}.{extension}"}}
if os.path.exists(f"{config['previews-path']}/{urlcode}.{extension}"):
return {"status": "pass", "details": {"code": 0, "name":f"{urlcode}.{extension}", "result": f"{config['url']}/static/previews/{urlcode}.{extension}"}}
try:
audio_input = ffmpeg.input(answer['path'])
audio_cut = audio_input.audio.filter('atrim', duration=duration)
audio_output = ffmpeg.output(audio_cut, f"{config['previews-path']}/{urlcode}.{extension}", audio_bitrate=bitrate)
ffmpeg.run(audio_output)
helpers.deleteAudio(f"temp/{urlcode}.{extension}")
except Exception as e: return {"status": "error", "details": {"error_code": 102, "error_details": str(e), "result": f"{config['url']}/static/temp/{urlcode}.{extension}"}}
return {"status": "pass", "details": {"code": error_code, "name":f"{urlcode}.{extension}", "result": f"{config['url']}/static/previews/{urlcode}.{extension}"}}