made it so i just click file and paste YouTube url

Linux is amazing

#! /usr/bin/bash
echo "Enter a url"
read a

yt-dlp -x $a
  • TechnoCat@piefed.social
    link
    fedilink
    English
    arrow-up
    4
    ·
    2 hours ago

    Here is a script I wrote:

    ~/bin 0s  
    > cat vget  
    #!/usr/bin/env fish  
    yt-dlp --embed-metadata --write-subs --embed-subs --write-thumbnail --prefer-free-formats -f "[height<=1080]" $argv  
    
  • NorthWestWind@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    1 hour ago

    Guess we’re sharing scripts now. I have a script that downloads playlists as MP3s and keep an archive.

    #!/usr/bin/env sh
    
    browser_cookies="firefox:1cvnyph7.YouTube TV"
    
    download() {
    	url="https://www.youtube.com/playlist?list=%241"
    	dir=$2
    	archive_name=$3
    
    	yt-dlp -x --audio-format mp3 --embed-thumbnail --embed-metadata --cookies-from-browser "$browser_cookies" --download-archive "archives/$archive_name.txt" -P "$dir" -o "%(title)s.%(ext)s" "$url"
    }
    
    download PLPzniwWWCSjVQteWPqVvyu8SQsrStVYwZ high-quality-rips/ rips
    download PLPzniwWWCSjWZj3-DAOh8ZKrsVReP_Ksm good-playlist/ picks
    
  • hoppolito@mander.xyz
    link
    fedilink
    English
    arrow-up
    28
    ·
    5 hours ago

    Very happy you had fun making the little script! One thing that will become important pretty quick if you continue making these scripts is that it’s almost always better to wrap your variables in quotes - so it becomes yt-dlp -x “$a. It’s okay here but if you ever paste something that has a space in it, this will keep it together ‘as one’.

    If you want to expand your knowledge with this, some fruitful paths to go down are the following:

    • can you find a way to download multiple urls one after the other if you paste them all at once? (Multiple arguments)
    • can you find a way to ask the user for these multiple urls one after the other? (loops)
    • and can you find a way to have it ask until you hit enter without a url pasted and only then it starts? (conditionals and test)

    The last one is already quite a bit advanced but if you can do that you have enough of the ‘programming’ basics of the shell down to a degree that you can create many little helpers like this with ease.

    Of course don’t feel forced to do any of that - if you’re happy with the improvement as-is, that’s all you need to enjoy the fun of Linux!

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      3
      ·
      edit-2
      1 hour ago

      One thing that will become important pretty quick if you continue making these scripts is that it’s almost always better to wrap your variables in quotes - so it becomes yt-dlp -x “$a.

      Oh man, this reminds me of the joke that any program that’s more complex than Hello World has bugs – and folks still don’t even agree how to spell “Hello, World!”.

      Of course, Bash is a particular minefield in this regard…

      • 18107@aussie.zone
        link
        fedilink
        English
        arrow-up
        4
        ·
        57 minutes ago

        I once wrote a 2 line, 10 word script that had 9 bugs in it. I’m not overly proud of that one.

  • BananaTrifleViolin@lemmy.world
    link
    fedilink
    English
    arrow-up
    5
    ·
    edit-2
    3 hours ago

    That’s great! Here’s a few tips to take it a bit further; the world is your oyster!

    Open your .bashrc file (e.g. /home/yourusername/.bashrc) and add the following:

    alias get="/path/to/your/bash/file"

    Now open a terminal and type get, and it’ll launch the script. No clicking needed, it’ll run anytime from any terminal!

    And if you do use the alias then you can use another refinement, you can drop the echo: instead of $a, you can use $1 and remove the echo & read as you no longer need them:

    #! /usr/bin/bash yt-dlp -x $1

    Now for example you can type in a terminal:

    get http://url.to.video/

    And yt-dlp will do it’s stuff. $1 passes the first parameter after starting the script as a variable to it.

    You can use the keyboard shortcut Control+shift+v to paste a URL into the terminal, no mouse needed; just remember to add a space after typing get

    • 0t79JeIfK01RHyzo@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      3 hours ago

      What does your ~/.bashrc look like? My last change was modifying a playlist command

      spoiler: I explain my last change to my ~/.bashrc file
      playlist https://www.youtube.com/@YouTube/videos
      

      or

      playlist /home/username/Videos
      

      or just from any directory with files

      playlist
      

      And then takes all the videos found at the url or at the path (including within folders), adds them to a playlist, shuffles them, and plays them from mpv.

      playlist() {
              param=""
      
              # If the first parameter has a length more than 1 character
              if [ ${#1} -gt 1 ]; then
                      param="${@}"
              else
                      param="."
              fi
      
              screen mpv $param --shuffle --ytdl-raw-options-add=cookies-from-browser=firefox --loop-playlist=inf --no-keepaspect-window --no-auto-window-resize
      }
      
      other functions and aliases in my ~/.bashrc
      alias code=codium
      alias files=nautilus
      alias explorer=nautilus
      alias rust="/path/to/.cargo/bin/evcxr"
      alias sniffnet="export ICED_BACKEND=tiny-skia; /path/to/.cargo/bin/sniffnet"
      alias http-server='/path/to/.cargo/bin/miniserve'
      alias iphone='uxplay'
      alias airplay='uxplay'
      alias watch='screen mpv --ytdl-raw-options-add=remote-components=ejs:github --ytdl-raw-options-add=cookies-from-browser=firefox --no-keepaspect-window '
      alias twitch='watch'
      alias timeshift-launcher="pkexec env WAYLAND_DISPLAY='$WAYLAND_DISPLAY' XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' /usr/bin/timeshift-launcher"
      alias update="sudo apt update && sudo apt upgrade -y && sudo flatpak update -y && sudo snap refresh"
      alias resize="path/to/resize/videos/resize.sh"
      
      playlist() {
              param=""
      
              # If the first parameter has a length more than 1 character
              if [ ${#1} -gt 1 ]; then
                      param="${@}"
              else
                      param="."
              fi
      
              screen mpv $param --shuffle --ytdl-raw-options-add=cookies-from-browser=firefox --loop-playlist=inf --no-keepaspect-window --no-auto-window-resize
      }
      
      gif() { ffmpeg -i $1 -f yuv4mpegpipe - | gifski -o $2 ${@:3} -;}
      
  • EccTM@lemmy.ml
    link
    fedilink
    English
    arrow-up
    2
    ·
    4 hours ago

    I have a similar scriptlet that I use to open YouTube URLs in mpv, using just and wl-clipboard… I just copy the URL and press my G1 key (it has a keybind of just yt-paste attached) which launches the yt-paste snippet below, reads the url from the clipboard, parses it and passes it to mpv.

    # Parse the clipboard for YouTube URLs and open them in mpv
    yt-paste:
      #!/usr/bin/env bash
      YOUTUBE_URL_REGEX="^https:\/\/(www\.youtube\.com\/watch\?v=|youtu\.be\/)[a-zA-Z0-9_-]{11}"
      YOUTUBE_PLAYLIST_URL_REGEX="^https:\/\/(www\.youtube\.com\/playlist\?list=)[a-zA-Z0-9_-]+"
      YOUTUBE_SHORTS_URL_REGEX="^https:\/\/(www\.youtube\.com\/shorts\/)[a-zA-Z0-9_-]{11}"
      # Youtube URL
      if [[ "$(wl-paste)" =~ $YOUTUBE_URL_REGEX ]]; then
        echo "Opening valid YouTube URL" >&2
        notify-send --app-name="YT-Paste" --icon=mpv --transient "Opening YouTube URL"
        mpv "$(wl-paste)"
      # Youtube Playlist URL
      elif [[ "$(wl-paste)" =~ $YOUTUBE_PLAYLIST_URL_REGEX ]]; then
        echo "Opening valid YouTube Playlist URL" >&2
        notify-send --app-name="YT-Paste" --icon=mpv --transient "Opening YouTube Playlist URL"
        mpv "$(wl-paste)"
      # Youtube Short URL
      elif [[ "$(wl-paste)" =~ $YOUTUBE_SHORTS_URL_REGEX ]]; then
        echo "Opening valid YouTube Shorts URL" >&2
        notify-send --app-name="YT-Paste" --icon=mpv --transient "Opening YouTube Shorts URL"
        mpv "$(wl-paste)"
      # No Match
      else
        echo "Clipboard does not contain a valid YouTube URL" >&2
        notify-send --app-name="YT-Paste" --icon=mpv --transient "Whoops!" "Clipboard does not contain a valid YouTube URL"
        exit 1
      fi