Terminal-Based YouTube Player

Jul 14, 2024

Terminal-Based YouTube Player

Introduction

  • Motivation: Simplify YouTube experience by running entirely from terminal
  • Initial Problem: Handling videos with Python using a single-thread model

Development Breakdown

Initial Setup

  • Central Point: Playing videos using command line arguments
    • Arguments: URL, scaling factor, max width
  • Libraries Used: yt_dlp for video stream
  • Core Process:
    1. Retrieves video stream URL
    2. Reads each frame sequentially
    3. Converts frames into ASCII
    4. Prints frames to console, refreshing each time

Frame Conversion

  • Frame-to-ASCII Converter: Key Component
    • Converts frames to grayscale
    • Resizes based on scaling factor and max width
    • Converts pixel groups into ASCII characters
  • Intensity Calculation: Key Method
    • Normalized pixel group values for character selection

Handling Terminal Window Resizing

  • Initial Problem: Poor handling of window resizing
  • Solution: Poll CLI dimensions on each frame render to update internal states

Performance Optimization

  • Issue: FPS depended on frame size
  • Solution:
    • Retrieve target framerate
    • Calculate minimum frame rendering interval

User Input

Thread Handling

  • Current Model: Single-threaded, requiring key input for rendering each frame
  • Solution: Move rendering to a separate thread to handle state and input

CLI Interaction

  • Utilized Function: getch() (captures input efficiently)
  • Special Consideration: Handling ctrl-c and ctrl-z
  • Key Features Added:
    • Pause/unpause video with a button
    • Display status: video playing, playback progress, creator's name, view count

Expanding Functionality

Refactoring for Future Expansion

  • Refactoring: Used callbacks to keep functions loosely coupled

Display Manager

  • Changes Implemented:
    • Managed a series of screens with custom rendering methods
    • Added responsive menu navigation bar
    • Auto-paused video when switching screens

YouTube API Integration

  • OAuth Authentication:
    • Steps: Google Cloud Console setup, downloaded credentials
    • Created AuthManager for login and credential storage
  • Service Handlers:
    • YoutubeConnectionManager managing internal states
    • YoutubeStateBase class for common logic in different states
  • Screens Implemented:
    • Home Page: Most popular videos due to lack of home API endpoint
    • Creator Page: Videos from the same creator
    • Search Page: Search for videos with a search bar

Rendering Screens

  • General Approach:
    • Convert thumbnails to ASCII, add titles and creator's name
  • Navigation:
    • Handlers for navigating through videos
    • Custom search bar with search functionality

Additional Fixes

  • Responsiveness Issue: Fixed terminal resize handling using separate thread
  • OAuth Usage: Integrated like and subscribe functionality
  • Accurate Aspect Ratio: Adjusted based on character dimensions
  • ASCII Character Set: Extended for better conversion accuracy