File size: 5,702 Bytes
5f88e7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import uuid
import asyncio
import subprocess
import shutil
from fastapi import FastAPI, Query, HTTPException
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(title="M3U8 to MP4 Converter", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

TEMP_DIR = "/tmp/m3u8_conversions"
os.makedirs(TEMP_DIR, exist_ok=True)

active_conversions = {}


def cleanup_files(file_paths):
    for path in file_paths:
        try:
            if os.path.exists(path):
                if os.path.isdir(path):
                    shutil.rmtree(path)
                else:
                    os.remove(path)
                logger.info(f"Cleaned up: {path}")
        except Exception as e:
            logger.error(f"Cleanup error for {path}: {e}")


def get_ffmpeg_path():
    ffmpeg_path = shutil.which("ffmpeg")
    if ffmpeg_path:
        return ffmpeg_path
    for path in ["/usr/bin/ffmpeg", "/usr/local/bin/ffmpeg", "/app/ffmpeg", "./ffmpeg"]:
        if os.path.exists(path):
            return path
    return "ffmpeg"


async def convert_m3u8_to_mp4(m3u8_url: str, output_path: str, task_id: str):
    ffmpeg = get_ffmpeg_path()
    
    cmd = [
        ffmpeg,
        "-i", m3u8_url,
        "-c", "copy",
        "-bsf:a", "aac_adtstoasc",
        "-movflags", "+faststart",
        "-y",
        output_path
    ]
    
    cmd_fallback = [
        ffmpeg,
        "-i", m3u8_url,
        "-c:v", "libx264",
        "-preset", "fast",
        "-crf", "23",
        "-c:a", "aac",
        "-b:a", "128k",
        "-movflags", "+faststart",
        "-y",
        output_path
    ]
    
    logger.info(f"Starting conversion: {task_id}")
    logger.info(f"Command: {' '.join(cmd)}")
    
    active_conversions[task_id] = {"status": "converting", "progress": 0}
    
    try:
        process = await asyncio.create_subprocess_exec(
            *cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        
        stdout, stderr = await process.communicate()
        
        if process.returncode != 0:
            logger.warning(f"Copy mode failed, trying re-encode mode: {stderr.decode()[:500]}")
            active_conversions[task_id]["status"] = "re-encoding"
            
            process = await asyncio.create_subprocess_exec(
                *cmd_fallback,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE
            )
            stdout, stderr = await process.communicate()
            
            if process.returncode != 0:
                raise Exception(f"Conversion failed: {stderr.decode()[:1000]}")
        
        active_conversions[task_id]["status"] = "completed"
        logger.info(f"Conversion completed: {task_id}")
        
    except Exception as e:
        active_conversions[task_id]["status"] = "failed"
        active_conversions[task_id]["error"] = str(e)
        logger.error(f"Conversion error: {e}")
        raise


@app.get("/")
def root():
    return {
        "message": "M3U8 to MP4 Converter API",
        "usage": "/video?url=<m3u8-url>&format=mp4",
        "endpoints": {
            "convert": "/video?url=<m3u8-url>&format=mp4",
            "status": "/status/<task_id>"
        }
    }


@app.get("/video")
async def convert_video(
    url: str = Query(..., description="M3U8 URL to convert"),
    format: str = Query("mp4", description="Output format (mp4 only)")
):
    if not url:
        raise HTTPException(status_code=400, detail="URL parameter is required")
    
    if not url.startswith(("http://", "https://")):
        raise HTTPException(status_code=400, detail="Invalid URL format")
    
    task_id = str(uuid.uuid4())
    output_filename = f"video_{task_id}.mp4"
    output_path = os.path.join(TEMP_DIR, output_filename)
    
    try:
        await convert_m3u8_to_mp4(url, output_path, task_id)
        
        if not os.path.exists(output_path):
            raise HTTPException(status_code=500, detail="Conversion failed - output file not created")
        
        file_size = os.path.getsize(output_path)
        logger.info(f"File size: {file_size} bytes")
        
        response = FileResponse(
            path=output_path,
            media_type="video/mp4",
            filename=f"converted_video_{task_id}.mp4",
            headers={
                "Content-Disposition": f'attachment; filename="converted_video_{task_id}.mp4"'
            }
        )
        
        @response.background
        def cleanup():
            cleanup_files([output_path])
        
        return response
        
    except HTTPException:
        cleanup_files([output_path])
        raise
    except Exception as e:
        cleanup_files([output_path])
        logger.error(f"Error: {e}")
        raise HTTPException(status_code=500, detail=f"Conversion error: {str(e)}")


@app.get("/status/{task_id}")
def get_status(task_id: str):
    if task_id not in active_conversions:
        return {"status": "not_found"}
    return active_conversions[task_id]


@app.get("/health")
def health_check():
    ffmpeg = get_ffmpeg_path()
    ffmpeg_available = os.path.exists(ffmpeg) or shutil.which("ffmpeg") is not None
    return {
        "status": "healthy",
        "ffmpeg_available": ffmpeg_available,
        "ffmpeg_path": ffmpeg
    }


if __name__ == "__main__":
    import uvicorn
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)