Pushp0120 commited on
Commit
2459767
·
verified ·
1 Parent(s): fb0e602

Update mirror.py

Browse files
Files changed (1) hide show
  1. mirror.py +21 -16
mirror.py CHANGED
@@ -1,6 +1,6 @@
1
  import sys
2
  import os
3
- import subprocess
4
  import pickle
5
  from googleapiclient.discovery import build
6
  from googleapiclient.http import MediaFileUpload
@@ -9,6 +9,9 @@ from google.auth.transport.requests import Request
9
  TOKEN_FILE = '/app/token.pickle'
10
  VIDEO_PATH = '/app/mirror_video.mp4'
11
 
 
 
 
12
  def get_credentials():
13
  creds = None
14
  if os.path.exists(TOKEN_FILE):
@@ -21,28 +24,30 @@ def get_credentials():
21
  return creds
22
 
23
  def download_from_youtube(video_id, output_path):
 
 
 
24
  url = f"https://www.youtube.com/watch?v={video_id}"
25
  print(f"Downloading: {url}")
26
 
27
  if os.path.exists(output_path):
28
  os.remove(output_path)
29
 
30
- result = subprocess.run([
31
- 'yt-dlp',
32
- '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
33
- '--merge-output-format', 'mp4',
34
- '-o', output_path,
35
- '--no-playlist',
36
- '--extractor-args', 'youtube:player_client=ios',
37
- '--user-agent', 'com.google.ios.youtube/19.29.1 (iPhone16,2; U; CPU iOS 17_5_1 like Mac OS X;)',
38
- '--add-header', 'X-Youtube-Client-Name:5',
39
- '--add-header', 'X-Youtube-Client-Version:19.29.1',
40
- url
41
- ], capture_output=True, text=True)
42
-
43
- if result.returncode != 0:
44
- raise Exception(f"yt-dlp failed: {result.stderr}")
45
 
 
 
46
  print("Download complete!")
47
 
48
  def upload_to_channel_b(title, description):
 
1
  import sys
2
  import os
3
+ import ssl
4
  import pickle
5
  from googleapiclient.discovery import build
6
  from googleapiclient.http import MediaFileUpload
 
9
  TOKEN_FILE = '/app/token.pickle'
10
  VIDEO_PATH = '/app/mirror_video.mp4'
11
 
12
+ # Fix SSL issues globally
13
+ ssl._create_default_https_context = ssl._create_unverified_context
14
+
15
  def get_credentials():
16
  creds = None
17
  if os.path.exists(TOKEN_FILE):
 
24
  return creds
25
 
26
  def download_from_youtube(video_id, output_path):
27
+ from pytubefix import YouTube
28
+ from pytubefix.cli import on_progress
29
+
30
  url = f"https://www.youtube.com/watch?v={video_id}"
31
  print(f"Downloading: {url}")
32
 
33
  if os.path.exists(output_path):
34
  os.remove(output_path)
35
 
36
+ yt = YouTube(url, on_progress_callback=on_progress, use_oauth=False, allow_oauth_cache=False)
37
+ print(f"Title: {yt.title}")
38
+
39
+ # Get highest resolution progressive stream (audio+video together)
40
+ stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').last()
41
+
42
+ if not stream:
43
+ # Fallback to any mp4
44
+ stream = yt.streams.filter(file_extension='mp4').first()
45
+
46
+ if not stream:
47
+ raise Exception("No suitable stream found!")
 
 
 
48
 
49
+ print(f"Downloading stream: {stream.resolution} {stream.mime_type}")
50
+ stream.download(filename=output_path)
51
  print("Download complete!")
52
 
53
  def upload_to_channel_b(title, description):