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
+
!pip install -U tqdm requests
|
| 3 |
+
!apt install ffmpeg -y
|
| 4 |
+
|
| 5 |
+
# π Imports
|
| 6 |
+
import os
|
| 7 |
+
import subprocess
|
| 8 |
+
import re
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
import requests
|
| 12 |
+
|
| 13 |
+
# π
Time Formatter
|
| 14 |
+
def now(): return datetime.now().strftime('%H:%M:%S')
|
| 15 |
+
|
| 16 |
+
# β
STEP 1: Download Video from Direct Link
|
| 17 |
+
def download_video_from_url(url, output_path):
|
| 18 |
+
print(f"\n⬠[{now()}] Downloading video from URL...")
|
| 19 |
+
response = requests.get(url, stream=True)
|
| 20 |
+
total_size = int(response.headers.get("content-length", 0))
|
| 21 |
+
block_size = 1024
|
| 22 |
+
with open(output_path, "wb") as f, tqdm(
|
| 23 |
+
desc="Downloading",
|
| 24 |
+
total=total_size,
|
| 25 |
+
unit="B",
|
| 26 |
+
unit_scale=True,
|
| 27 |
+
unit_divisor=1024,
|
| 28 |
+
) as bar:
|
| 29 |
+
for data in response.iter_content(block_size):
|
| 30 |
+
f.write(data)
|
| 31 |
+
bar.update(len(data))
|
| 32 |
+
file_size_mb = os.path.getsize(output_path) / (1024*1024)
|
| 33 |
+
print(f"β
[{now()}] Download complete! File Size: {file_size_mb:.2f} MB")
|
| 34 |
+
|
| 35 |
+
# β
STEP 2: Get Duration
|
| 36 |
+
def get_video_duration(input_path):
|
| 37 |
+
result = subprocess.run(
|
| 38 |
+
["ffprobe", "-v", "error", "-show_entries", "format=duration",
|
| 39 |
+
"-of", "default=noprint_wrappers=1:nokey=1", input_path],
|
| 40 |
+
stdout=subprocess.PIPE,
|
| 41 |
+
stderr=subprocess.STDOUT
|
| 42 |
+
)
|
| 43 |
+
return float(result.stdout)
|
| 44 |
+
|
| 45 |
+
# β
STEP 3: FAST 3GP Conversion (MPEG-4 + AAC)
|
| 46 |
+
def convert_to_3gp_with_progress(input_path, output_path):
|
| 47 |
+
print(f"\n㪠[{now()}] Converting to 3GP (FAST MODE)...")
|
| 48 |
+
duration = get_video_duration(input_path)
|
| 49 |
+
|
| 50 |
+
cmd = [
|
| 51 |
+
"ffmpeg",
|
| 52 |
+
"-i", input_path,
|
| 53 |
+
"-vf", "scale=320:240",
|
| 54 |
+
"-c:v", "mpeg4", "-b:v", "250k",
|
| 55 |
+
"-c:a", "aac", "-b:a", "65k",
|
| 56 |
+
"-preset", "ultrafast",
|
| 57 |
+
"-threads", "0",
|
| 58 |
+
"-y", output_path
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
| 62 |
+
time_pattern = re.compile(r'time=(\d+):(\d+):(\d+\.\d+)')
|
| 63 |
+
progress_bar = tqdm(total=duration, unit='s', desc="β‘ Converting", ncols=90)
|
| 64 |
+
|
| 65 |
+
for line in process.stdout:
|
| 66 |
+
match = time_pattern.search(line)
|
| 67 |
+
if match:
|
| 68 |
+
h, m, s = match.groups()
|
| 69 |
+
current = int(h)*3600 + int(m)*60 + float(s)
|
| 70 |
+
progress_bar.n = current
|
| 71 |
+
progress_bar.refresh()
|
| 72 |
+
|
| 73 |
+
process.wait()
|
| 74 |
+
progress_bar.n = duration
|
| 75 |
+
progress_bar.refresh()
|
| 76 |
+
progress_bar.close()
|
| 77 |
+
|
| 78 |
+
if process.returncode == 0:
|
| 79 |
+
file_size_mb = os.path.getsize(output_path) / (1024*1024)
|
| 80 |
+
print(f"β
[{now()}] FAST Conversion Finished! File Size: {file_size_mb:.2f} MB")
|
| 81 |
+
else:
|
| 82 |
+
print(f"β [{now()}] Conversion Failed!")
|
| 83 |
+
|
| 84 |
+
# β
STEP 4: Upload to BuzzHeavier
|
| 85 |
+
def upload_to_buzzheavier(file_path, file_name, account_id, parent_id):
|
| 86 |
+
print(f"\nπ€ [{now()}] Uploading to BuzzHeavier...")
|
| 87 |
+
|
| 88 |
+
file_size = os.path.getsize(file_path)
|
| 89 |
+
print(f"π¦ File Size: {file_size / (1024*1024):.2f} MB")
|
| 90 |
+
upload_url = f"https://w.buzzheavier.com/{parent_id}/{file_name}"
|
| 91 |
+
headers = {"Authorization": f"Bearer {account_id}"}
|
| 92 |
+
|
| 93 |
+
with open(file_path, "rb") as f, tqdm(
|
| 94 |
+
desc="Uploading",
|
| 95 |
+
total=file_size,
|
| 96 |
+
unit="B",
|
| 97 |
+
unit_scale=True,
|
| 98 |
+
unit_divisor=1024,
|
| 99 |
+
) as bar:
|
| 100 |
+
class ProgressFile:
|
| 101 |
+
def __init__(self, file, bar):
|
| 102 |
+
self.file = file
|
| 103 |
+
self.bar = bar
|
| 104 |
+
def read(self, size):
|
| 105 |
+
data = self.file.read(size)
|
| 106 |
+
if data:
|
| 107 |
+
self.bar.update(len(data))
|
| 108 |
+
return data
|
| 109 |
+
def __getattr__(self, attr):
|
| 110 |
+
return getattr(self.file, attr)
|
| 111 |
+
|
| 112 |
+
pf = ProgressFile(f, bar)
|
| 113 |
+
upload_resp = requests.put(upload_url, headers=headers, data=pf)
|
| 114 |
+
|
| 115 |
+
try:
|
| 116 |
+
resp_json = upload_resp.json()
|
| 117 |
+
except:
|
| 118 |
+
resp_json = {}
|
| 119 |
+
|
| 120 |
+
if upload_resp.status_code in [200, 201] and "id" in resp_json:
|
| 121 |
+
file_id = resp_json["id"]
|
| 122 |
+
print(f"β
Upload complete! File ID: {file_id}")
|
| 123 |
+
|
| 124 |
+
# Fetch file info for download link
|
| 125 |
+
api_url = f"https://buzzheavier.com/api/fs/{file_id}"
|
| 126 |
+
r = requests.get(api_url, headers=headers)
|
| 127 |
+
|
| 128 |
+
if r.status_code == 200:
|
| 129 |
+
file_info = r.json()
|
| 130 |
+
if "link" in file_info:
|
| 131 |
+
print(f"π Download Link: {file_info['link']}")
|
| 132 |
+
else:
|
| 133 |
+
print("β οΈ Link not found in response:", file_info)
|
| 134 |
+
else:
|
| 135 |
+
print("β οΈ Failed to get file info:", r.text)
|
| 136 |
+
else:
|
| 137 |
+
print("β Upload Failed! Status:", upload_resp.status_code, "Response:", resp_json)
|
| 138 |
+
|
| 139 |
+
# π RUN FULL PIPELINE
|
| 140 |
+
video_url = "https://pub-f85680743e6f48c3b210af18613f778c.r2.dev/5f863c1a1eaf3435ce92423ef1962e45?t=1780377844&token=cb4f6b3e112f9a90d03296fe59da77e8d9c498d89238d4b4226b0090fb3721a9" # replace with your direct video link
|
| 141 |
+
mp4_path = "/content/b-89~.mp4"
|
| 142 |
+
gp3_path = "/content/b-89~.3gp"
|
| 143 |
+
gp3_name = "b-89~.3gp"
|
| 144 |
+
|
| 145 |
+
buzz_account_id = "97WK8Y7GZY22EP1X9YLN" # replace with your BuzzHeavier token
|
| 146 |
+
buzz_parent_id = "ox9lutl74y33" # replace with your folder ID
|
| 147 |
+
|
| 148 |
+
print("π Starting Direct Video Pipeline (FAST MODE)...\n")
|
| 149 |
+
|
| 150 |
+
download_video_from_url(video_url, mp4_path)
|
| 151 |
+
convert_to_3gp_with_progress(mp4_path, gp3_path)
|
| 152 |
+
upload_to_buzzheavier(gp3_path, gp3_name, buzz_account_id, buzz_parent_id)
|
| 153 |
+
|
| 154 |
+
print(f"\nβ
[{now()}] EVERYTHING DONE! β‘ Blazing Fast π")
|