YT-DLP and FFMPEG standalone video downloader

I am trying to create a video downloader, that can download only from youtube.com and vimeo.com
I can get the script to work fine on my machine, both running from IDE and the pyinstaller .exe, but running the .exe I created on another machine gives a FileNotFoundError.

I am still relatively new to coding, so may be something obvious I am missing, or some knowledge that I need to go and learn about. Any advice towards this is appreciated

I have tried including the ffmpeg.exe and yt-dlp.exe in their own folders, so folder structure is as follows:

main.exe
ffmpeg
ffmpeg.exe
yt-dlp
yt-dlp.exe

I am using main.spec to build the executable with pyinstaller, I have included both main.py and main.spec below

main.py:
import os
import platform
import subprocess
import sys

def get_videos_path():
    if platform.system() == 'Windows':
        return os.path.join(os.environ['USERPROFILE'], 'Videos')
    elif platform.system() == 'Darwin':
        return os.path.join(os.environ['HOME'], 'Movies')
    else:
        raise Exception('Unsupported operating system.')

def sanitize_filename(filename):
    return "".join(c for c in filename if c.isalnum() or c in (' ', '.', '_')).rstrip()

def get_ffmpeg_path():
    # find the ffmpeg executable. 
    if getattr(sys, 'frozen', False):
        # If the application is run as a bundle
        if platform.system() == 'Windows':
            return os.path.join(sys._MEIPASS, 'ffmpeg', 'ffmpeg.exe')
        else:
            return os.path.join(sys._MEIPASS, 'ffmpeg', 'ffmpeg')
    else:
        # If the application is run as a script
        if platform.system() == 'Windows':
            return os.path.join(os.path.dirname(__file__), 'ffmpeg', 'ffmpeg.exe')
        else:
            return 'ffmpeg'  # Adjust for other systems if necessary

def download_video(url, output_path):
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    # Construct yt-dlp command
    command = [
        'yt-dlp', 
        '--impersonate', 'Safari',  # Ensure impersonation target is specified
        '--format', 'bestvideo+bestaudio/best',  # Format selection
        '--merge-output-format', 'mp4',  # Merge formats into mp4
        '--ffmpeg-location', get_ffmpeg_path(),  # Specify FFmpeg location
        '--output', os.path.join(output_path, '%(title)s.%(ext)s'),  # Output template
        url
    ]

    # Ensure FFmpeg is correctly detected
    ffmpeg_path = get_ffmpeg_path()
    print(f"FFmpeg path: {ffmpeg_path}")
    os.environ['PATH'] += os.pathsep + os.path.dirname(ffmpeg_path)
    print(f'''Video downloading now, and will be output to {output_path}.
        The console will close automatically when the video is downloaded.''')
    try:
        # Execute the yt-dlp command
        subprocess.run(command, check=True)
        print(f"Video downloaded and merged successfully to {output_path}")
    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")

# Actual script, get user input and run the commands
video_url = ""
while 'youtube.com' not in video_url.lower() and "vimeo.com" not in video_url.lower():
    video_url = input("Please paste the video url here. Youtube or Vimeo only: ")
output_path = get_videos_path()
download_video(video_url, output_path)

main.spec:
# –– mode: python ; coding: utf-8 –

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('ffmpeg', 'ffmpeg')],
    hiddenimports=['yt_dlp'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

New contributor

will beard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật