diff --git a/utils/ffmpeg_install.py b/utils/ffmpeg_install.py index 7d8acc5..094a660 100644 --- a/utils/ffmpeg_install.py +++ b/utils/ffmpeg_install.py @@ -1,3 +1,4 @@ +import zipfile import requests import os import subprocess @@ -5,30 +6,50 @@ import subprocess def ffmpeg_install_windows(): try: - zip = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip" - r = requests.get(zip) - with open("ffmpeg.zip", "wb") as f: + ffmpeg_url = "https://github.com/GyanD/codexffmpeg/releases/download/6.0/ffmpeg-6.0-full_build.zip" + ffmpeg_zip_filename = "ffmpeg.zip" + ffmpeg_extracted_folder = "ffmpeg" + + # Check if ffmpeg.zip already exists + if os.path.exists(ffmpeg_zip_filename): + os.remove(ffmpeg_zip_filename) + + # Download FFmpeg + r = requests.get(ffmpeg_url) + with open(ffmpeg_zip_filename, "wb") as f: f.write(r.content) - import zipfile - with zipfile.ZipFile("ffmpeg.zip", "r") as zip_ref: + # Check if the extracted folder already exists + if os.path.exists(ffmpeg_extracted_folder): + # Remove existing extracted folder and its contents + for root, dirs, files in os.walk(ffmpeg_extracted_folder, topdown=False): + for file in files: + os.remove(os.path.join(root, file)) + for dir in dirs: + os.rmdir(os.path.join(root, dir)) + os.rmdir(ffmpeg_extracted_folder) + + # Extract FFmpeg + with zipfile.ZipFile(ffmpeg_zip_filename, "r") as zip_ref: zip_ref.extractall() os.remove("ffmpeg.zip") - os.rename("ffmpeg-master-latest-win64-gpl", "ffmpeg") - # Move the files inside bin to the root - for file in os.listdir("ffmpeg/bin"): - os.rename(f"ffmpeg/bin/{file}", f"./{file}") - os.rmdir("ffmpeg/bin") - for file in os.listdir("ffmpeg/doc"): - os.remove(f"ffmpeg/doc/{file}") - os.rmdir("ffmpeg/doc") - os.remove("ffmpeg/LICENSE.txt") - os.rmdir("ffmpeg/") - print( - "FFmpeg installed successfully! Please restart your computer and then re-run the program." - ) + # Rename and move files + os.rename(f"{ffmpeg_extracted_folder}-6.0-full_build", ffmpeg_extracted_folder) + for file in os.listdir(os.path.join(ffmpeg_extracted_folder, "bin")): + os.rename(os.path.join(ffmpeg_extracted_folder, "bin", file), os.path.join(".", file)) + os.rmdir(os.path.join(ffmpeg_extracted_folder, "bin")) + for file in os.listdir(os.path.join(ffmpeg_extracted_folder, "doc")): + os.remove(os.path.join(ffmpeg_extracted_folder, "doc", file)) + for file in os.listdir(os.path.join(ffmpeg_extracted_folder, "presets")): + os.remove(os.path.join(ffmpeg_extracted_folder, "presets", file)) + os.rmdir(os.path.join(ffmpeg_extracted_folder, "presets")) + os.rmdir(os.path.join(ffmpeg_extracted_folder, "doc")) + os.remove(os.path.join(ffmpeg_extracted_folder, "LICENSE")) + os.remove(os.path.join(ffmpeg_extracted_folder, "README.txt")) + os.rmdir(ffmpeg_extracted_folder) + print("FFmpeg installed successfully! Please restart your computer and then re-run the program.") except Exception as e: print( "An error occurred while trying to install FFmpeg. Please try again. Otherwise, please install FFmpeg manually and try again." @@ -75,16 +96,12 @@ def ffmpeg_install_mac(): def ffmpeg_install(): try: # Try to run the FFmpeg command - subprocess.run( - ["ffmpeg", "-version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) + subprocess.run(['ffmpeg', '-version'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except FileNotFoundError as e: # Check if there's ffmpeg.exe in the current directory if os.path.exists("./ffmpeg.exe"): - print( - "FFmpeg is installed on this system! If you are seeing this error for the second time, restart your computer." - ) - print("FFmpeg is not installed on this system.") + print('FFmpeg is installed on this system! If you are seeing this error for the second time, restart your computer.') + print('FFmpeg is not installed on this system.') resp = input( "We can try to automatically install it for you. Would you like to do that? (y/n): " ) @@ -97,7 +114,9 @@ def ffmpeg_install(): elif os.name == "mac": ffmpeg_install_mac() else: - print("Your OS is not supported. Please install FFmpeg manually and try again.") + print( + "Your OS is not supported. Please install FFmpeg manually and try again." + ) exit() else: print("Please install FFmpeg manually and try again.")