File size: 1,937 Bytes
68707b3
66ca64a
 
 
 
 
 
 
7814f6f
66ca64a
 
367be45
66ca64a
 
 
 
367be45
 
 
66ca64a
367be45
 
792e427
f02b1de
66ca64a
792e427
66ca64a
68707b3
 
 
66ca64a
 
 
792e427
66ca64a
792e427
367be45
792e427
66ca64a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 }}, 400
    urlcode = answer['urlcode']
    bitrate = answer['bitrate']
    error_code = answer['ytdlp-code']
    
    duration = helpers.getFromRequest(request, "duration")
    try: duration = int(duration) if duration.isnumeric() and int(duration) <= 90 else 45
    except: duration = 45
    
    extension = helpers.getFromRequest(request, "extension")
    if not extension: 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}"}}, 400
    return {"status": "pass", "details": {"code": error_code, "name":f"{urlcode}.{extension}", "result": f"{config['url']}/static/previews/{urlcode}.{extension}"}}