-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_context_menu.py
84 lines (67 loc) · 2.79 KB
/
video_context_menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Description: A Python script to add a context menu item to Explorer, that will download videos from most Video Sites using yt-dlp.
import os
import sys
import time
import subprocess
def install_package(package_name):
print(f"{package_name} not found, installing...")
time.sleep(1)
# Download Package That Isn't Installed.
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
try:
import yt_dlp
except ImportError:
install_package("yt-dlp")
import yt_dlp
try:
import winreg as reg
except ImportError:
install_package("winreg")
import winreg as reg
try:
from tkinter import Tk, simpledialog
except ImportError:
install_package("tkinter")
from tkinter import Tk, simpledialog
# Function to add the command to the context menu
def add_to_context_menu():
application_path = os.path.abspath(sys.argv[0])
# Notice the "%V" passed as an argument in the command, which Windows replaces with the clicked directory path
key_path = r'Directory\\Background\\shell\\DownloadVideo'
command_path = r'Directory\\Background\\shell\\DownloadVideo\\command'
key = reg.CreateKey(reg.HKEY_CLASSES_ROOT, key_path)
reg.SetValue(key, '', reg.REG_SZ, '&Download Video')
reg.CloseKey(key)
command_key = reg.CreateKey(reg.HKEY_CLASSES_ROOT, command_path)
reg.SetValue(command_key, '', reg.REG_SZ, f'{sys.executable} "{application_path}" "%V"')
reg.CloseKey(command_key)
def install_ffmpeg():
subprocess.run(["winget", "install", "ffmpeg"])
try:
result = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
if result.returncode != 0:
install_ffmpeg()
except OSError as e:
install_ffmpeg()
print("Context menu item added.")
# Function to display the URL input dialog and download the video
def download_video_dialog(directory):
root = Tk()
root.withdraw() # Hide the Tkinter root window
video_url = simpledialog.askstring("Download Video", "Enter the Video URL:")
if video_url:
# Change to the directory where the context menu was clicked
os.chdir(directory)
# Assuming yt-dlp is installed and added to PATH
subprocess.run(["yt-dlp", video_url])
print('You can safely close this window now.')
input("Press Enter to exit...")
# Main execution logic
if __name__ == "__main__":
# Check if "install" is in the command line arguments
if len(sys.argv) > 1 and sys.argv[1] == "--install":
add_to_context_menu()
else:
# The directory path should be the second argument, if present
directory = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
download_video_dialog(directory)