Offex commited on
Commit
5deb27a
·
verified ·
1 Parent(s): 0a869f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -41
app.py CHANGED
@@ -16,7 +16,7 @@ def load_model():
16
  print("✅ Model Loaded!")
17
  return model
18
 
19
- # --- 2. URL Resolver (Short -> Long) ---
20
  def get_actual_url(short_url):
21
  try:
22
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
@@ -25,33 +25,25 @@ def get_actual_url(short_url):
25
  except:
26
  return short_url
27
 
28
- # --- 3. Process Video (Download & Transcribe) ---
29
  def process_video(url):
30
  if not url:
31
  return None, "⚠️ URL missing!"
32
 
33
- print(f"Processing: {url}")
34
-
35
- # 1. Resolve URL
36
  actual_url = get_actual_url(url)
 
37
 
38
- # 2. Download Video (MP4)
39
- output_file = "tiktok_video.mp4"
40
-
41
- # Purani file safai
42
- if os.path.exists(output_file): os.remove(output_file)
43
 
44
  ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
45
 
46
  ydl_opts = {
47
- 'format': 'best[ext=mp4]/best', # <-- Ab hum Video download kar rahe hain
48
- 'outtmpl': "tiktok_video.%(ext)s",
 
49
  'ffmpeg_location': ffmpeg_path,
50
- 'quiet': False,
51
- 'no_warnings': True,
52
- 'nocheckcertificate': True,
53
  'http_headers': {
54
- 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
55
  'Referer': 'https://www.tiktok.com/'
56
  }
57
  }
@@ -59,44 +51,34 @@ def process_video(url):
59
  try:
60
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
61
  ydl.download([actual_url])
 
 
 
62
  except Exception as e:
63
- return None, f"❌ Download Error: {str(e)}"
64
-
65
- if not os.path.exists(output_file):
66
- return None, "❌ Video download failed."
67
 
68
- # 3. Transcribe (Audio from Video)
69
  try:
70
  current_model = load_model()
71
- # Whisper direct video file se audio padh lega
72
- segments, _ = current_model.transcribe(output_file, beam_size=1)
73
  text = " ".join([s.text for s in segments])
74
-
75
- # Return Video Path AND Transcript Text
76
- return output_file, text
77
  except Exception as e:
78
- return output_file, f"Transcription Error: {str(e)}"
79
 
80
- # --- 4. UI Design ---
81
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
82
  gr.Markdown("# 🎵 TikTok Downloader & Transcriber")
83
- gr.Markdown("Link paste karo -> Video download karo -> Text copy karo (Ek sath!)")
84
 
85
  with gr.Row():
86
- link_input = gr.Textbox(label="TikTok URL", placeholder="Paste link here...", scale=4)
87
- btn = gr.Button("🚀 Process", variant="primary", scale=1)
88
 
89
  with gr.Row():
90
- # Left Side: Video Player
91
- with gr.Column():
92
- video_out = gr.Video(label="Download Video", format="mp4")
93
-
94
- # Right Side: Code Box for Easy Copy
95
- with gr.Column():
96
- # 'gr.Code' use kiya hai taki 1-click copy button mile
97
- transcript_out = gr.Code(label="Transcript (Click Copy Icon 📋)", language=None, lines=10)
98
 
99
- # Button Action
100
  btn.click(fn=process_video, inputs=link_input, outputs=[video_out, transcript_out])
101
 
102
  demo.launch()
 
16
  print("✅ Model Loaded!")
17
  return model
18
 
19
+ # --- 2. URL Resolver ---
20
  def get_actual_url(short_url):
21
  try:
22
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
 
25
  except:
26
  return short_url
27
 
28
+ # --- 3. Process Video & Transcript ---
29
  def process_video(url):
30
  if not url:
31
  return None, "⚠️ URL missing!"
32
 
 
 
 
33
  actual_url = get_actual_url(url)
34
+ output_video = "final_video.mp4"
35
 
36
+ if os.path.exists(output_video): os.remove(output_video)
 
 
 
 
37
 
38
  ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
39
 
40
  ydl_opts = {
41
+ 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
42
+ 'outtmpl': 'downloaded_video.%(ext)s',
43
+ 'merge_output_format': 'mp4', # ब्राउज़र संगतता के लिए पक्का MP4
44
  'ffmpeg_location': ffmpeg_path,
 
 
 
45
  'http_headers': {
46
+ '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',
47
  'Referer': 'https://www.tiktok.com/'
48
  }
49
  }
 
51
  try:
52
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
53
  ydl.download([actual_url])
54
+ # डाउनलोड की गई फ़ाइल का नाम बदलें
55
+ downloaded_file = next(f for f in os.listdir('.') if f.startswith('downloaded_video'))
56
+ shutil.move(downloaded_file, output_video)
57
  except Exception as e:
58
+ return None, f"❌ Error: {str(e)}"
 
 
 
59
 
60
+ # Transcribe
61
  try:
62
  current_model = load_model()
63
+ segments, _ = current_model.transcribe(output_video, beam_size=1)
 
64
  text = " ".join([s.text for s in segments])
65
+ return output_video, text
 
 
66
  except Exception as e:
67
+ return output_video, f"Transcription Error: {str(e)}"
68
 
69
+ # --- 4. UI ---
70
+ with gr.Blocks(theme="soft") as demo:
71
  gr.Markdown("# 🎵 TikTok Downloader & Transcriber")
 
72
 
73
  with gr.Row():
74
+ link_input = gr.Textbox(label="TikTok URL", placeholder="लिंक यहाँ पेस्ट करें...")
75
+ btn = gr.Button("🚀 Start", variant="primary")
76
 
77
  with gr.Row():
78
+ video_out = gr.Video(label="Video")
79
+ # 'gr.Code' में कॉपी बटन पहले से मौजूद होता है
80
+ transcript_out = gr.Code(label="Transcript (Copy करने के लिए ऊपर दाईं ओर क्लिक करें)", language="markdown")
 
 
 
 
 
81
 
 
82
  btn.click(fn=process_video, inputs=link_input, outputs=[video_out, transcript_out])
83
 
84
  demo.launch()