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

top 44 comments

sorted by: hot top controversial new old
[–] 64 points 8 months ago (4 children)

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!

  • source
  • hideshow 4 child comments
  • [–] 8 points 8 months ago* (2 children)

    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...

  • source
  • parent
  • hideshow 2 child comments
  • [–] 50 points 8 months ago (1 child)

    Stop right now. This will all end in tears. You’ll become a developer and spend the rest of your life fixing bugs. You can still get out.

  • source
  • hideshow 1 child comment
  • [–] 18 points 8 months ago (2 children)
    [–] 18 points 8 months ago (1 child)

    You all forgot to add the best yt-dlp option: --sponsorblock-remove all

  • source
  • hideshow 1 child comment
  • [–] 15 points 8 months ago

    One of us! One of us!

  • source
  • [–] 10 points 8 months ago
    [–] 8 points 8 months ago (3 children)

    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  
    
  • source
  • hideshow 3 child comments
  • [–] 2 points 8 months ago (2 children)

    does --write-thumbnail save a jpg with the video? or is it something else?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 8 points 8 months 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
    
  • source
  • [–] 4 points 8 months ago (2 children)

    this isn't perfect but i made one when i wanted to fetch a video for a specific resolution (because i prefer 480)

    ytgrab() {  
      local id="$1"  
      local res="${2:-480}" # default to 480  
      local url="https://www.youtube.com/watch?v=$id"  
    
      # fetch formats  
      local fmt  
      fmt=$(yt-dlp -F --cookies-from-browser vivaldi "$url")  
    
      # printing the format output  
      echo "$fmt"  
    
      # pick video format matching the requested resolution  
      local vfmt  
      vfmt=$(echo "$fmt" | awk -v r="${res}p" '$0 ~ r && /video/ {print $1}' | head -n1)  
    
      # pick best m4a audio  
      local afmt  
      afmt=$(echo "$fmt" | awk '/m4a/ && /audio/ {print $1}' | head -n1)  
    
      # safety check  
      if [ -z "$vfmt" ] || [ -z "$afmt" ]; then  
        echo "Could not find matching formats (video ${res}p or m4a audio)."  
        return 1  
      fi  
    
      echo fetching: yt-dlp -f ${vfmt}+${afmt} --cookies-from-browser vivaldi --write-subs --no-write-auto-subs --sub-lang "en.*" $url  
      yt-dlp -f "${vfmt}+${afmt}" --write-subs --cookies-from-browser vivaldi --no-write-auto-subs --sub-lang "en.*" "$url"  
    }  
    
  • source
  • hideshow 2 child comments
  • [–] 2 points 8 months ago (1 child)
  • usually i just like older videos but in this case i was saving a bunch of wcw vault videos to my jellyfin library and i prefer 480 since it was as close to tv as can be (also i've never been a fan of hd and tv after 2000 because i felt that's when it went downhill)

  • source
  • parent
  • [–] 4 points 8 months ago* (2 children)

    You've got me beat. I just have a text file with some common usage examples in it.

  • source
  • hideshow 2 child comments
  • [–] 7 points 8 months ago* (last edited 8 months ago) (6 children)

    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

  • source
  • hideshow 6 child comments
  • [–] 2 points 8 months ago* (last edited 8 months ago) (4 children)

    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} -;}
    

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 8 months ago (3 children)

    I wonder if we have the same resize.sh

  • source
  • parent
  • hideshow 3 child comments
  • [–] 1 point 8 months ago (2 children)

    The version I have was copied from stackoverflow. It doesn't work very well, it makes a rough estimate to get the video file size under the set value. As an example

    resize video.mp4 10
    

    Which then resizes the video to 10 megabytes if possible.

    resize.sh code

    file=$1
    target_size_mb=$2  # target size in MB
    target_size=$(( $target_size_mb * 1000 * 1000 * 8 )) # target size in bits
    length=`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file"`
    length_round_up=$(( ${length%.*} + 1 ))
    total_bitrate=$(( $target_size / $length_round_up ))
    audio_bitrate=$(( 128 * 1000 )) # 128k bit rate
    video_bitrate=$(( $total_bitrate - $audio_bitrate ))
    ffmpeg -i "$file" -b:v $video_bitrate -maxrate:v $video_bitrate -bufsize:v $(( $target_size / 20 )) -b:a $audio_bitrate "${file}-${target_size_mb}mb.mp4"
    

    I'll probably replace it eventually.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 8 months ago* (1 child)

    Definitely not the same lol

    Mine uses ffmpeg to change the resolution, it doesn't so much care about file sizes.

    It could be a one-liner if you only ever feed it a single file to manipulate..

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 8 months ago

    I might add one for scaling. I just don't use it as frequently as trying to meet a file size limit. The scaling is also much easier to remember

    ffmpeg -i  in.mp4 -vf "scale=600:-1" -an out.mp4
    

    It does get complicated though, when scaling many videos and images, I've used something like the following in the past

    find .  -exec ffmpeg -i {} -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1:color=black" {}.mp4 \;
    

    Those were the only two that showed up when I typed history | grep scale.

    after commenting, I also added a new video file resizer.

    It works significantly better than the one I previously posted. It's also copied from stackoverflow.

    bitrate="$(awk "BEGIN {print int($2 * 1024 * 1024 * 8 / $(ffprobe \
        -v error \
        -show_entries format=duration \
        -of default=noprint_wrappers=1:nokey=1 \
        "$1" \
    ) / 1000)}")k"
    ffmpeg \
        -y \
        -i "$1" \
        -c:v libx264 \
        -preset medium \
        -b:v $bitrate \
        -pass 1 \
        -an \
        -f mp4 \
        /dev/null \
    && \
    ffmpeg \
        -i "$1" \
        -c:v libx264 \
        -preset medium \
        -b:v $bitrate \
        -pass 2 \
        -an \
        "${1%.*}-$2mB.mp4"
    

  • source
  • parent
  • [–] 3 points 8 months 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
    
  • source
  • [–] 3 points 8 months ago (2 children)

    No offense, but I'm seeing a lot of useless scripts here. You can simply put these option in yt-dlp.conf and then just run yt-dlp "url".

  • source
  • hideshow 2 child comments
  • [–] [S] 1 point 8 months ago (1 child)

    I made this for easy use so no typing or terminal. some people like to do thing differently and that's ok

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 8 months ago

    When you read a, you're typing in terminal. I made a clipboard action so I just copy an URL to the clipboard, execute actions, and then choose whether to play it or download it on my other machine.

  • source
  • parent
  • [–] 3 points 8 months ago

    I self host Pinchflat and have set it up to monitor one of my own yt playlists. Then if I want to download anything on mobile or desktop I just save it to that playlist and it's done.

  • source
  • [–] 3 points 8 months ago

    I love things like this, makes it so easy to learn when it's a really simple to understand and explicit implementation of a high level feature (read input, pass to command)

  • source
  • [–] 2 points 8 months ago
    [–] 2 points 8 months ago*

    Nice! I did mine as an alias:

    alias yt='yt-dlp --remote-components ejs:github -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4"'

    And then just yt [URL] downloads the thing.

  • source
  • [–] 1 point 8 months ago

    this is neat, thanks!

  • source
  • [–] 1 point 8 months ago

    awesome! I never would have thought to make something like that.

  • source
  • [–] 1 point 8 months ago*

    You could make it an alias and shorten the number of keystrokes

    I prefer keeping my aliases in ~/.bash_aliases, which is sourced in my ~/.bashrc, ie

    . ~/.bash_aliases
    

    Then you would just need to source your bashrc to load it the first time.

  • source
  • [–] -4 points 8 months ago

    I simply using local LLM for it

  • source