File size: 1,453 Bytes
d5cff7d
 
 
7a2ad2e
d5cff7d
 
7a2ad2e
 
 
 
 
 
 
 
 
 
 
 
fc6f959
 
 
 
 
 
 
 
15e9a2c
fc6f959
15e9a2c
fc6f959
 
 
 
 
 
 
 
 
 
15e9a2c
 
 
 
fc6f959
 
 
 
 
15e9a2c
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import yt_dlp
import os
import uuid

from openai import OpenAI

USE_OPENAI = False  # True → OpenAI Whisper, False → Nebius

if USE_OPENAI:
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    STT_MODEL = "gpt-4o-transcribe"
else:
    client = OpenAI(
        base_url="https://api.tokenfactory.nebius.com/v1/",
        api_key=os.environ["NEBIUS_API_KEY"]
    )
    STT_MODEL = "gpt-whisper-1"

def transcribe_from_url(url: str) -> str:
    """
    Stable IG/TikTok/YT reel transcribe
    → forced audio extraction MP3
    → removes video-only DASH problem
    """
    if not os.getenv("OPENAI_API_KEY"):
        raise RuntimeError("Brak OPENAI_API_KEY")

    tmp = f"/tmp/{uuid.uuid4().hex}.mp3"

    ydl_opts = {
        "format": "bestaudio/best",          # nie mp4 — tylko audio
        "postprocessors": [{
            "key": "FFmpegExtractAudio",
            "preferredcodec": "mp3",
            "preferredquality": "192"
        }],
        "outtmpl": tmp.replace(".mp3", ".%(ext)s"),
        "quiet": True,
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])

    audio_file = tmp
    if not os.path.exists(audio_file):
        raise RuntimeError("Audio extraction failed – IG returned no audio stream")

    with open(audio_file, "rb") as f:
        transcript = client.audio.transcriptions.create(
            model="gpt-4o-transcribe",
            file=f
        )

    return transcript.text