File size: 2,315 Bytes
ee0f024
 
 
 
5c2c779
ee0f024
5c2c779
ee0f024
5c2c779
0ad42da
5c2c779
ee0f024
 
5c2c779
ee0f024
 
5c2c779
0ad42da
ee0f024
0ad42da
 
 
 
 
 
 
 
 
 
 
 
 
 
ee0f024
0ad42da
ee0f024
 
 
0ad42da
ee0f024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c2c779
ee0f024
 
 
 
 
 
 
 
 
 
 
0ad42da
ee0f024
0ad42da
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import subprocess
import requests
import os
import time

app = FastAPI()

ASSEMBLYAI_API_KEY = os.getenv("ASSEMBLYAI_API_KEY")

class VideoURL(BaseModel):
    url: str

@app.post("/analyze")
def analyze_video(video: VideoURL):
    try:
        # 1. Download audio using yt-dlp with modified cache location
        output_file = "audio.mp3"
        cmd = [
            "yt-dlp",
            "-x",
            "--audio-format", "mp3",
            "--no-cache-dir",  # Disable cache to avoid permission issues
            "--force-ipv4",    # Force IPv4 to avoid network issues
            "-o", output_file,
            video.url
        ]
        
        # Add cookies if available (you'll need to provide a cookies.txt file)
        if os.path.exists("cookies.txt"):
            cmd.extend(["--cookies", "cookies.txt"])
            
        result = subprocess.run(cmd, capture_output=True, text=True)
        
        if result.returncode != 0:
            raise Exception(f"Download error: {result.stderr}")

        # Rest of your code remains the same...
        # 2. Upload audio to AssemblyAI
        headers = {"authorization": ASSEMBLYAI_API_KEY}
        with open(output_file, "rb") as f:
            upload_response = requests.post(
                "https://api.assemblyai.com/v2/upload",
                headers=headers,
                files={"file": f}
            )
        audio_url = upload_response.json()["upload_url"]

        # 3. Send transcription request
        transcript_response = requests.post(
            "https://api.assemblyai.com/v2/transcript",
            json={"audio_url": audio_url},
            headers=headers
        )
        transcript_id = transcript_response.json()["id"]

        # 4. Poll for completion
        while True:
            poll = requests.get(f"https://api.assemblyai.com/v2/transcript/{transcript_id}", headers=headers)
            status = poll.json()["status"]
            if status == "completed":
                return {"transcription": poll.json()["text"]}
            elif status == "error":
                raise Exception("Transcription error")
            time.sleep(3)
            
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))