reedmayhew commited on
Commit
6841e1f
·
verified ·
1 Parent(s): e23ed2f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import uuid
4
+ import glob
5
+ import shutil
6
+ import zipfile
7
+ import subprocess
8
+
9
+ import gradio as gr
10
+
11
+ def backup_tiktok(url):
12
+ """
13
+ Downloads TikTok video(s) from a provided URL using yt-dlp.
14
+ If multiple videos are downloaded (e.g., from a TikTok profile),
15
+ return a zip of all videos. Otherwise, return the single video file.
16
+ """
17
+
18
+ # Validate URL
19
+ if "tiktok.com" not in url.lower():
20
+ return "Please provide a valid TikTok URL (must contain 'tiktok.com')."
21
+
22
+ # Create a unique temporary folder
23
+ download_id = str(uuid.uuid4())
24
+ download_folder = f"downloads_{download_id}"
25
+ os.makedirs(download_folder, exist_ok=True)
26
+
27
+ # Run yt-dlp command
28
+ cmd = ["yt-dlp", url, "-o", f"{download_folder}/%(title)s.%(ext)s"]
29
+ try:
30
+ subprocess.run(cmd, check=True)
31
+ except subprocess.CalledProcessError as e:
32
+ return f"Error downloading: {str(e)}"
33
+
34
+ # Gather all downloaded files
35
+ video_files = glob.glob(f"{download_folder}/*")
36
+
37
+ if len(video_files) == 0:
38
+ return "No videos found."
39
+
40
+ # If only one video, return it directly
41
+ if len(video_files) == 1:
42
+ return video_files[0]
43
+
44
+ # Otherwise, zip all files and return the zip
45
+ zip_name = f"tiktok_backup_{download_id}.zip"
46
+ with zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED) as zipf:
47
+ for file_path in video_files:
48
+ arcname = os.path.basename(file_path)
49
+ zipf.write(file_path, arcname=arcname)
50
+
51
+ return zip_name
52
+
53
+
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown("# TikTok Backup\n\nEnter a TikTok URL to download the video(s).")
56
+
57
+ with gr.Row():
58
+ url_input = gr.Textbox(label="TikTok URL", placeholder="https://www.tiktok.com/@username/video/...")
59
+ download_button = gr.Button("Download")
60
+
61
+ output_file = gr.File(label="Downloaded File or ZIP")
62
+
63
+ download_button.click(fn=backup_tiktok, inputs=url_input, outputs=output_file)
64
+
65
+ demo.launch()