You are not logged in.

#1 2019-02-17 06:18:56

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,733

drop url from youtube to be played in mpv (gui)

Handy gui droplet for droping urls/files to mpv playlist. More than 30 minutes in making and more than 5 days in testing now. Especially nice to overcome some chrome/chromium shift+drop trouble.
drop here not

Last edited by brontosaurusrex (2019-05-01 23:27:51)

Offline

#2 2019-02-17 06:45:22

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,229
Website

Re: drop url from youtube to be played in mpv (gui)

Neat!
Does just what it says.

What was it you couldn't do on Chromium? (I use Firefox.)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#3 2019-02-17 08:44:42

ohnonot
...again
Registered: 2015-09-29
Posts: 3,671
Website

Re: drop url from youtube to be played in mpv (gui)

I particularly like the history future.

But since 99% of these URLs are copied from my main browser, I don't have much need for it, because of the glorious Open with addon (also available for chrom* iirc). I could simply point it to mpv, but I have a custom script for that. I might add the history functionality to it.

Offline

#4 2019-02-17 11:28:49

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,733

Re: drop url from youtube to be played in mpv (gui)

@johnraff; Shift + drag and drop from youtube to mpv does add url to playlist with ff, doesn't seem to work with chrome (it will override currently playing clip).

Offline

#5 2019-02-18 07:25:53

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,229
Website

Re: drop url from youtube to be played in mpv (gui)

brontosaurusrex wrote:

Shift + drag and drop from youtube to mpv does add url to playlist with ff

So it does! I've never thought to do that, but it would save you from having to keep a bunch of tabs open I guess. You can drag links straight out of a forum post too. I usually have Fx maximized though, so it's a little bit messy having to go down to tint2 midway to find mpv. Your app scores there because it's always on top.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#6 2019-02-18 08:55:06

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,733

Re: drop url from youtube to be played in mpv (gui)

I wonder if it's possible to use part of tint2 as dropable thingy?

Btw there is slight concept of 'session' going on here, like 'session' is active until that pipe-controled-mpv instance is opened, on the other hand one can close and reopen the drop as needed (will still be the same session).

Last edited by brontosaurusrex (2019-02-18 15:37:07)

Offline

#7 2019-02-19 01:22:29

misko_2083
Member
Registered: 2016-05-24
Posts: 164

Re: drop url from youtube to be played in mpv (gui)

Can you make this work with the drop and html in a paned dialog?
Some videos work some don't with this (depending on the link):

#!/bin/bash

# drop

# Drop stuff to be played in mpv to playlist
# This is workaround for chrome not supporting shift+drag behavior

# needs: yad, mpv, youtube-dl, socat, awk, mediainfo

# cli switches: -i, -l, -h

# checks
command -v yad >/dev/null 2>&1 || { echo "I need yad, exiting." >&2; exit 1; }
command -v mpv >/dev/null 2>&1 || { echo "I need mpv, exiting." >&2; exit 1; }
command -v youtube-dl >/dev/null 2>&1 || { echo "I need youtube-dl, exiting." >&2; exit 1; }
command -v socat >/dev/null 2>&1 || { echo "I need socat, exiting." >&2; exit 1; }
command -v awk >/dev/null 2>&1 || { echo "I need awk, exiting." >&2; exit 1; }
command -v mediainfo >/dev/null 2>&1 || { echo "I need mediainfo, exiting." >&2; exit 1; }

# vars
pipe="/tmp/pipe" && touch "$pipe"
log="$HOME/.watched.m3u"

# logging
if ! [ -f "$log" ]; then
    # start fresh
    printf "#EXTM3U\n\n" > "$log" # If this fails, mpv can still play playlist without the header.
fi
touch "$log" || echo "Can't write to $log"

# mpv and pipe
isRunning () {

    # lets query whatever to see if the thing is already running
    if [ ! -f "$pipe" ] && (echo '{ "command": ["get_property", "mpv-version"] }' | socat - "$pipe") &> /dev/null ; then 
        #echo "Mpv instance already running"
        true
    else
        echo "Mpv not running, starting now"
        
        mpv --cache-secs=35 --idle --force-window --video-zoom=0.03 --autofit-larger=50% --geometry 49% --input-ipc-server="$pipe" --ytdl-format='bestvideo[ext=mp4][width<=1920][height<=1200][fps<=30]+bestaudio[ext=m4a]' &
        sleep 1 # naive approach to wait until pipe is ready
    fi
}

# awk_round
awk_round () {
    awk 'BEGIN{printf "%."'$1'"f\n", "'$2'"}'
}

# cli option -i or --info
whatPlaying () {
    
    # exit if pipe not working
    echo '{ "command": ["get_property", "media-title"] }' | socat - "$pipe" >/dev/null 2>&1 || exit 

    # get data
    title="$(echo '{ "command": ["get_property", "media-title"] }' | socat - "$pipe" | jq -r .data )" 
    percent="$(echo '{ "command": ["get_property", "percent-pos"] }' | socat - "$pipe" | jq -r .data )"
    percent="$(awk_round 0 "$percent")"
    #timepos="$(echo '{ "command": ["get_property", "playback-time"] }' | socat - "$pipe" | jq -r .data )"
    #timerem="$(echo '{ "command": ["get_property", "playtime-remaining"] }' | socat - "$pipe" | jq -r .data )"
    playlistpos="$(echo '{ "command": ["get_property", "playlist-pos-1"] }' | socat - "$pipe" | jq -r .data )"
    playlistcount="$(echo '{ "command": ["get_property", "playlist-count"] }' | socat - "$pipe" | jq -r .data )"
    
    echo "$title ($playlistpos/$playlistcount) $percent%"

}

if [ "$1" == "-i" ] || [ "$1" == "--info" ]; then

    whatPlaying && exit
    
elif [ "$1" == "-l" ] || [ "$1" == "--log" ]; then

    tail -n 12 "$log"
    whatPlaying && exit
    
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    echo "drop                  # start droplet gui"
    echo "drop -i or --info     # show what's playing"
    echo "drop -l or --log      # tail log and show what's playing"
    echo "drop -h or --help     # this help" 
    
    exit

fi

# gui action
yad --plug=$$ \
    --tabnum=1 \
    --on-top --no-buttons --title="drop urls here" --text="\n\n\nDROP" --geometry=200x200+50-50 --text-align=center --dnd | while read -r url

    do

        isRunning
        
        echo "$url just droped"
        
        # load stuff to mpv pipe
        echo "loadfile \"$url\" append-play" | socat - "$pipe"
        
        # url or file
        if [[ $url == http* ]] ; then 
        
            # output of command to array
            # mapfile -t line < <(some command)
            mapfile -t line < <(youtube-dl -e --get-duration "$url")

            # Array to vars
            yt="$(echo "${line[0]}" | tr '-' '|')"
            dur="${line[1]}"

            # inform
            echo "$yt | $dur"
            
            # to seconds for the playlist
            dursec="$(echo "$dur" | awk -F\: '{ for(k=NF;k>0;k--) sum+=($k*(60^(NF-k))); print sum }')"
            
            desc="$yt"
        
        else # assume its a file 
        
            # Must be file, not a dir
            if [[ -f "${url//file:\/\/}" ]]; then
                true
          
            else
                echo "$url is not file"
                continue
            fi
            
            # Just the file.ext part
            desc="${url##*/}"
            
            # For info in HH:MM:SS:FF
            dur="$(mediainfo "--Inform=General;%Duration/String4%" "$url")"
            
            # inform
            echo "$desc | $dur"
            
            # For playlist
            # miliseconds
            dursec="$(mediainfo "--Inform=General;%Duration%" "$url")"
            # to seconds and awk round
            dursec="$(echo "scale=2;$dursec/1000" | bc)"
            dursec="$(awk_round 0 "$dursec")"
            

        fi

        # log
        echo "#EXTINF:$dursec, $desc | $dur" >> "$log"
        printf "%s\n\n" "$url" >> "$log"
        
        # reset vars
        yt="" ; desc="" ; url=""
        
       
        
        
    done &

yad --plug=$$ \
    --tabnum=2 \
    --html \
    --uri="https://www.youtube.com"  \
    --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" &

yad --no-escape \
    --paned \
    --title "yt to mpv" \
    --key=$$ \
    --orient=hor \
    --splitter=100 \
    --width=600 \
    --height=500

Offline

#8 2019-02-19 06:30:29

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,733

Re: drop url from youtube to be played in mpv (gui)

@misko_2083; Have some example(s) of the ones that don't?

Offline

Board footer

Powered by FluxBB