infinitel8p / youtube-dl

Simple YouTube Downloader with GUI, work in progress
1 stars 0 forks source link

rewrite code base #3

Closed infinitel8p closed 1 year ago

infinitel8p commented 1 year ago
import os
import pytube
import urllib.request
import logging
import tkinter as tk
from tkinter import filedialog
from moviepy.editor import VideoFileClip, ImageClip

import CustomTkinter as ct

# Set up the logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_format = logging.Formatter('%(asctime)s: %(message)s')

# Create a handler to display log messages in the GUI
class TkinterHandler(logging.Handler):
    def __init__(self, text_widget):
        logging.Handler.__init__(self)
        self.text_widget = text_widget

    def emit(self, record):
        self.text_widget.insert(tk.END, log_format.format(record))
        self.text_widget.see(tk.END)

# Create the GUI
root = tk.Tk()
root.title('YouTube Downloader')

# Set up the log output widget
log_output = ct.CustomText(root)
log_output.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# Add the TkinterHandler to the logger
handler = TkinterHandler(log_output)
logger.addHandler(handler)

# Set up the URL input widget
url_input = ct.CustomEntry(root)
url_input.pack(side=tk.LEFT)

# Set up the download button
def download():
    # Get the URL from the input widget
    url = url_input.get()
    logger.info(f'Downloading video from {url}')

    # Create a YouTube object
    yt = pytube.YouTube(url)

    # Get the first video available
    video = yt.streams.first()

    # Choose a file to save the video
    file_path = filedialog.asksaveasfilename(defaultextension='.mp4')
    logger.info(f'Saving video to {file_path}')

    # Download the video
    video.download(file_path)
    logger.info('Video downloaded')

    # Download the thumbnail image
    thumbnail_url = video.thumbnail_url
    thumbnail_path = file_path.replace('.mp4', '.jpg')
    urllib.request.urlretrieve(thumbnail_url, thumbnail_path)

    # Load the video and the thumbnail image into MoviePy
    video_clip = VideoFileClip(file_path)
    thumbnail_clip = ImageClip(thumbnail_path)

    # Add the thumbnail image as the cover image for the video
    final_clip = video_clip.set_cover(thumbnail_clip)

    # Save the modified video
    final_clip.write_videofile(file_path)
    logger.info('Cover image added')

    # Clean up
    os.remove(thumbnail_path)

download_button = ct.CustomButton(root, text='Download', command=download)
download_button.pack(side=tk.LEFT)

# Run the GUI
root.mainloop()
import os
import pytube
import tkinter as tk
from tkinter import filedialog

import CustomTkinter as ct

# Set up the logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_format = logging.Formatter('%(asctime)s: %(message)s')

# Create a handler to display log messages in the GUI
class TkinterHandler(logging.Handler):
    def __init__(self, text_widget):
        logging.Handler.__init__(self)
        self.text_widget = text_widget

    def emit(self, record):
        self.text_widget.insert(tk.END, log_format.format(record))
        self.text_widget.see(tk.END)

# Create the GUI
root = tk.Tk()
root.title('YouTube Downloader')

# Set up the log output widget
log_output = ct.CustomText(root)
log_output.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# Add the TkinterHandler to the logger
handler = TkinterHandler(log_output)
logger.addHandler(handler)

# Set up the URL input widget
url_input = ct.CustomEntry(root)
url_input.pack(side=tk.LEFT)

# Set up the download button
def download():
    # Get the URL from the input widget
    url = url_input.get()
    logger.info(f'Downloading video from {url}')

    # Create a YouTube object
    yt = pytube.YouTube(url)

    # Get the first video available
    video = yt.streams.first()

    # Choose a file to save the video
    file_path = filedialog.asksaveasfilename(defaultextension='.mp4')
    logger.info(f'Saving video to {file_path}')

    # Download the video
    video.download(file_path)
    logger.info('Video downloaded')

download_button = ct.CustomButton(root, text='Download', command=download)
download_button.pack(side=tk.LEFT)

# Run the GUI
root.mainloop()
import urllib.request
from moviepy.editor import VideoFileClip

# Download the thumbnail image
thumbnail_url = video.thumbnail_url
thumbnail_path = file_path.replace('.mp4', '.jpg')
urllib.request.urlretrieve(thumbnail_url, thumbnail_path)

# Load the video and the thumbnail image into MoviePy
video_clip = VideoFileClip(file_path)
thumbnail_clip = ImageClip(thumbnail_path)

# Add the thumbnail image as the cover image for the video
final_clip = video_clip.set_cover(thumbnail_clip)

# Save the modified video
final_clip.write_videofile(file_path)

# Clean up
os.remove(thumbnail_path)
infinitel8p commented 1 year ago

Create a standalone executable for Mac using py2app:

Install py2app using the following command: pip install py2app Create a script called setup.py in the same directory as the script, with the following contents:

from setuptools import setup
setup(
    app=["script.py"],
    setup_requires=["py2app"],
)

Run the following command to create the executable: python setup.py py2app This will create a folder called dist that contains the executable.