Skip to main content

A command-line tool to convert YouTube videos to MP3 or MP4 using yt-dlp

Project description

ytconvert-cli

Production-ready YouTube to MP3/MP4 converter.

PyPI

PyPI: ytconvert-cli 1.0.1

Github: click here!

PyPI version Python 3.10+ License: MIT


🚀 Features

  • Convert YouTube videos to MP3 (audio extraction)
  • Download YouTube videos as MP4 (with quality selection)
  • Custom output directory support
  • Progress indicators with download speed and ETA
  • Clean error handling with specific exit codes
  • FFmpeg is bundled—no separate install needed
  • Uses yt-dlp Python API (no shell calls)
  • Pip-installable and PyPI-ready

📦 Installation

From PyPI (recommended)

pip install ytconvert-cli

From source

Fork https://github.com/Alkyones/ytconvert-cli

git clone https://github.com/Yourusername/ytconvert-cli
cd ytconvert-cli
pip install .

🖥️ CLI Usage

# Convert to MP3 (default)
ytconvert https://www.youtube.com/watch?v=VIDEO_ID

# Convert to MP4
ytconvert https://www.youtube.com/watch?v=VIDEO_ID --format mp4

# MP4 with specific quality
ytconvert https://www.youtube.com/watch?v=VIDEO_ID --format mp4 --quality 720p

# Save to custom directory
ytconvert https://www.youtube.com/watch?v=VIDEO_ID -f mp3 -o ./downloads

# Get video info only (no download)
ytconvert https://www.youtube.com/watch?v=VIDEO_ID --info

# Search YouTube and download selected result (interactive)
ytconvert search "lofi hip hop"

# Search and download selected result as MP3
ytconvert search "chill beats" --audio

# Search and download selected result as MP4
ytconvert search "chill beats" --video

# Show help
ytconvert --help

# Show search command help
ytconvert search --help

Short options:

Long Short Description
--format -f mp3 or mp4
--quality -q Video quality
--output -o Output directory
--info -i Info only
--verbose -v Debug mode
--version -V Show version

Search options:

Long Description
--limit Number of search results (default: 10)
--audio Download selected search result as MP3
--video Download selected search result as MP4

Search YouTube from the CLI

You can search YouTube directly in the terminal and choose what to download.

ytconvert search "lofi hip hop"

The command:

  1. Runs a YouTube search using yt-dlp
  2. Shows a numbered list with title, channel, and duration
  3. Prompts you to select a result number
  4. Downloads the selected video through the existing download pipeline

Example interaction:

$ ytconvert search "lofi hip hop"

[1] Lofi Hip Hop Radio - Beats to Relax/Study To
Channel: Lofi Girl
Duration: 3:12:00

[2] Chill Lofi Mix
Channel: Chillhop Music
Duration: 1:45:10

Select video number: 2

Audio/video examples:

ytconvert search "chill beats" --audio
ytconvert search "chill beats" --video
ytconvert search "study music" --limit 5

Examples

# Convert URL to MP3
ytconvert https://www.youtube.com/watch?v=VIDEO_ID

# Convert URL to MP4 at 720p
ytconvert https://www.youtube.com/watch?v=VIDEO_ID --format mp4 --quality 720p

# Search and download interactively
ytconvert search "lofi hip hop"

# Search and download selected result as MP3
ytconvert search "chill beats" --audio

# Search and download selected result as MP4
ytconvert search "chill beats" --video

🐍 Python API Usage

from ytconvert import YouTubeConverter

# Convert to MP3
converter = YouTubeConverter(output_dir="./downloads")
mp3_path = converter.convert_to_mp3("https://youtube.com/watch?v=VIDEO_ID")
print(f"Saved: {mp3_path}")

# Convert to MP4 with quality
mp4_path = converter.convert_to_mp4("https://youtube.com/watch?v=VIDEO_ID", quality="720p")

# Get video info only
info = converter.get_video_info("https://youtube.com/watch?v=VIDEO_ID")
print(f"Title: {info['title']}")
print(f"Duration: {info['duration']} seconds")

🛠️ Integration Example (Django, subprocess)

import subprocess
import sys

def convert_youtube_video(url: str, format: str = "mp3", quality: str = "best"):
    cmd = [
        sys.executable, "-m", "ytconvert.cli",
        url,
        "--format", format,
        "--output", "/path/to/downloads",
    ]
    if format == "mp4" and quality != "best":
        cmd.extend(["--quality", quality])
    result = subprocess.run(cmd, capture_output=True, text=True)
    if result.returncode == 0:
        return {"success": True, "output": result.stdout}
    else:
        return {"success": False, "exit_code": result.returncode, "error": result.stderr}

⚠️ Exit Codes

Code Meaning
0 Success
1 Invalid YouTube URL
2 Download failure
3 Format or quality unavailable
4 Unexpected error

🏗️ Project Structure

ytconvert-cli/
├── ytconvert/
│   ├── __init__.py      # Package exports
│   ├── cli.py           # CLI entry point
│   ├── converter.py     # yt-dlp logic
│   ├── validators.py    # URL/format validation
**PyPI:** [ytconvert-cli 1.0.2](https://pypi.org/project/ytconvert-cli/1.0.2/)
│   └── utils.py         # Utilities
├── pyproject.toml       # Packaging config
├── README.md            # This file
└── LICENSE              # MIT License

📝 Development

# Run tests
pytest

# Format code
black ytconvert/
ruff check ytconvert/ --fix

# Type check
mypy ytconvert/

📄 License

MIT License - see LICENSE


🙏 Acknowledgments


📢 Disclaimer

This tool is intended for downloading videos you have the right to download. Please respect copyright laws and YouTube's Terms of Service. The authors are not responsible for misuse.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ytconvert_cli-1.1.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ytconvert_cli-1.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file ytconvert_cli-1.1.0.tar.gz.

File metadata

  • Download URL: ytconvert_cli-1.1.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for ytconvert_cli-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f581fa8584748211c82305c45e74e00ad7d43631c9247858394fbe228b69a1ec
MD5 7c5db1d57df5e28ecda039c009b7f6aa
BLAKE2b-256 23258e21c323fb2f3046ad2a09a5936a1faa4b0aafb1c31bf8689f6cfd51b739

See more details on using hashes here.

File details

Details for the file ytconvert_cli-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ytconvert_cli-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for ytconvert_cli-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac68f04d7629b6d0cd00b02c7d822ff0839326300f95c03cbc82f66e270f7f4f
MD5 46a3323e0748a695e1e6f95b14a35f89
BLAKE2b-256 8181cea8c4f9581b2b39fb8d55800af9884fcc9809a929335921d4cdd634c146

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page