Tolimo commited on
Commit
f57b99d
·
verified ·
1 Parent(s): 581d4de

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +55 -9
main.py CHANGED
@@ -28,6 +28,24 @@ def get_file_size(url: str):
28
  return int(r.headers.get('Content-Length', 0))
29
  except: return 0
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  @app.post("/download-private")
32
  async def download_private_fb(payload: dict = Body(...)):
33
  html_source = payload.get("html", "")
@@ -62,20 +80,48 @@ async def download_private_fb(payload: dict = Body(...)):
62
  }
63
 
64
  @app.get("/process-video")
65
- async def process_video(url: str, res: str, background_tasks: BackgroundTasks):
66
  # အသုံးပြုသူက Convert လုပ်ခိုင်းသည့်အခါမှသာ Processing လုပ်ပေးမည့် logic
67
  file_id = str(uuid.uuid4())
68
  out_file = os.path.join(TEMP_DIR, f"{file_id}.mp4")
69
 
70
  # ဤနေရာတွင် အရှေ့က ကျွန်တော်တို့ အောင်မြင်ခဲ့သော Force Encoding Logic ကို သုံးပါမယ်
71
- # (မှတ်ချက် - ကုဒ်ရှည်မည်စိုး၍ အနှစ်ချုပ်ထားပါသည်)
72
- target_h = res.replace("p", "")
73
- subprocess.run([
74
- 'ffmpeg', '-y', '-i', url,
75
- '-vf', f'scale=w="trunc(oh*a/2)*2":h={target_h}',
76
- '-c:v', 'libx264', '-preset', 'ultrafast', '-crf', '24',
77
- '-c:a', 'aac', '-b:a', '128k', '-movflags', '+faststart', out_file
78
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  space_id = os.getenv('SPACE_ID', '').replace("/", "-")
81
  return {"download_url": f"https://{space_id}.hf.space/get_file?id={file_id}.mp4"}
 
28
  return int(r.headers.get('Content-Length', 0))
29
  except: return 0
30
 
31
+ # (Added from original) Required for the Force Encoding logic to work properly
32
+ def download_temp_file(url: str, suffix: str):
33
+ """ဗီဒီယို သို့မဟုတ် အသံ stream ကို server ထဲသို့ ဒေါင်းလုဒ်ဆွဲခြင်း"""
34
+ if not url: return None
35
+ path = os.path.join(TEMP_DIR, f"{uuid.uuid4()}{suffix}")
36
+ try:
37
+ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}
38
+ r = requests.get(url, stream=True, timeout=30, headers=headers)
39
+ if r.status_code == 200:
40
+ with open(path, 'wb') as f:
41
+ for chunk in r.iter_content(chunk_size=1024*1024):
42
+ f.write(chunk)
43
+ if os.path.getsize(path) > 1024:
44
+ return path
45
+ except: pass
46
+ if os.path.exists(path): os.remove(path)
47
+ return None
48
+
49
  @app.post("/download-private")
50
  async def download_private_fb(payload: dict = Body(...)):
51
  html_source = payload.get("html", "")
 
80
  }
81
 
82
  @app.get("/process-video")
83
+ async def process_video(url: str, res: str, background_tasks: BackgroundTasks, audio_url: str = None):
84
  # အသုံးပြုသူက Convert လုပ်ခိုင်းသည့်အခါမှသာ Processing လုပ်ပေးမည့် logic
85
  file_id = str(uuid.uuid4())
86
  out_file = os.path.join(TEMP_DIR, f"{file_id}.mp4")
87
 
88
  # ဤနေရာတွင် အရှေ့က ကျွန်တော်တို့ အောင်မြင်ခဲ့သော Force Encoding Logic ကို သုံးပါမယ်
89
+ target_h = res.replace("p", "") if res else "720"
90
+ v_tmp = download_temp_file(url, ".mp4")
91
+ a_tmp = download_temp_file(audio_url, ".mp3") if audio_url else None
92
+
93
+ try:
94
+ if v_tmp:
95
+ # Merging Streams & Forcing H.264 (avc1) + AAC...
96
+ if a_tmp:
97
+ cmd = [
98
+ 'ffmpeg', '-y', '-i', v_tmp, '-i', a_tmp,
99
+ '-map', '0:v:0', '-map', '1:a:0',
100
+ '-vf', f'scale=w="trunc(oh*a/2)*2":h={target_h}',
101
+ '-c:v', 'libx264', '-preset', 'fast', '-crf', '23',
102
+ '-c:a', 'aac', '-b:a', '128k',
103
+ '-movflags', '+faststart', out_file
104
+ ]
105
+ # Video only mode: Converting to H.264...
106
+ else:
107
+ cmd = [
108
+ 'ffmpeg', '-y', '-i', v_tmp,
109
+ '-vf', f'scale=w="trunc(oh*a/2)*2":h={target_h}',
110
+ '-c:v', 'libx264', '-preset', 'fast', '-crf', '23',
111
+ '-c:a', 'aac', '-movflags', '+faststart', out_file
112
+ ]
113
+
114
+ subprocess.run(cmd, check=True, capture_output=True)
115
+
116
+ if v_tmp: os.remove(v_tmp)
117
+ if a_tmp: os.remove(a_tmp)
118
+ else:
119
+ raise Exception("Video stream download failed.")
120
+ except Exception as e:
121
+ # Error တက်ပါက Temp File အပျက်အစီးများကို ရှင်းလင်းမည်
122
+ if v_tmp and os.path.exists(v_tmp): os.remove(v_tmp)
123
+ if a_tmp and os.path.exists(a_tmp): os.remove(a_tmp)
124
+ raise HTTPException(status_code=500, detail=f"Processing error: {str(e)}")
125
 
126
  space_id = os.getenv('SPACE_ID', '').replace("/", "-")
127
  return {"download_url": f"https://{space_id}.hf.space/get_file?id={file_id}.mp4"}