Skip Navigation
bash

Talk about the Bash Shell and Bash scripting

Members
60
Posts
1
Active Today
1
Created
1 yr. ago
  • bash @lemm.ee
    cmysmiaczxotoy @lemm.ee

    Bash script to download and search youtube subtitles and output clickable timestamped urls

    Here is the script.

     undefined
        
    
    #!/usr/bin/env bash
    # Download and search youtube subs
    # deps yt-dlp ,awk, perl, any one or more of either ugrep, ripgrep, grep
    # usage "script youtube_url"
    
    
    main() {
        url="$@"
        check_if_url
        get_video_id
        search_for_downloaded_matching_files
        set_download_boolean_flag
        download_subs
        read_and_format_transcript_file
        echo_description_file
        user_search
    }
    
    
    # Iterate over the array and add items to the new array if they match the regex
    check_if_url() {
        local regex='^https://[^[:space:]]+$'
            if ! [[ $url =~ $regex ]]; then
                echo "Invalid input. Valid input is a url matching regex ${regex}"
                exit 1
            fi
    }
    
    
    get_video_id() {
        video_id=$(echo "$url" | sed -n 's/.*v=\([^&]*\).*/\1/p')
    }
    
    
    search_for_downloaded_matching_files() {
        # Find newest created files matching the video_id
        transcript_file="$(  /usr/bin/ls -t --time=creation "$PWD"/*${video_id}*\.vtt 2>/dev/null | head -n 1  )"
        descr