You are not logged in.

#1 2016-02-25 11:57:00

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

m : finds and plays music [bash script]

I would very much like to share a BASH script I use daily,
which finds and plays my music very fast.

My Music library is well organized (folder and files well named),
and in my case, cmus has a too long keyboard workflow.
with m I can type

m radioh cr

and it will find and play instantly Radiohead - Creep  cool

So here it is, please critic  big_smile



Originally posted on gentoo forums, but I just registered for an other matter,
and arch forums are admittedly more lively for this kind of posts.

edit: moved from github to gitlab

Last edited by arnaudv6 (2018-06-08 07:50:17)

Offline

#2 2016-02-25 13:12:20

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,794
Website

Re: m : finds and plays music [bash script]

Make an Aur package, I'm too lazy


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#3 2016-02-25 13:31:58

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: m : finds and plays music [bash script]

Comments by line numbers:

30: Why do you say "no tilde please", it'd work just fine with a tilde, just get rid of the quotes, and/or just use $HOME

33: What is sentaku, I cannot find it anywhere in the repos or AUR

35: Why do you need a windowing toolkit?

36: Why do you need youtube-dl to play local music?  Have this test only under the conditional blocks that actually need youtube-dl

38: This line does nothing useful

39: Don't do this: like line 30 comment, just do it right in the first place.  Do not try to reinvent the wheel.

58: You concatenate all search parameters into a single word.  Your example of "m radioh cr" would make searchterm radiohcr.  Is this what you want?

62-65: Why not just have this in the case statement on line 55 to save a variable and a conditional test:
    *) echo "${help}"; exit ;;


86-91: Equivalent:
    find "${music_dir}" -printf "%P\n" | sort > "${index_file}"

~103: I don't get the logic.  You create a list of all files with find, then you grep that list for your match.  Just use find - again, don't try to reinvent the wheel.

The following seems to get the same results - just doesn't have the cli parameter checking and I didn't implement the youtube_dl stuff as I don't use it.

#!/bin/bash

search=('find ~/music -type f')
while [[ $# -gt 1 ]]; do
        search+=("-ipath \"*$1*\"")
        shift
done
search+=("-iname \"*$1*\"")

files=( $(eval ${search[@]}) )

for f in ${files[@]}; do mpv $f; done
[[ ${#files} -eq 0 ]] && echo "do youtube_dl stuff here"

(edit: oops, I forgot to replace my test "echo" with mpv.  Fixed)

EDIT 2: This was all being nitpicky.  It all looks good as it is - I hope my nitpicking is only seen as the constructive criticism it is intended to be in order to highlight possibilities in shell scripting.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2016-02-25 13:36:10

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,783
Website

Re: m : finds and plays music [bash script]

ugjka wrote:

Make an Aur package, I'm too lazy

That sort of attitude is uncalled for and not welcome. Laziness does not justify rudeness.

https://wiki.archlinux.org/index.php/Fo … ther_users


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#5 2016-02-25 14:03:52

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,794
Website

Re: m : finds and plays music [bash script]

Umm sorry if that sounded rude, I meant that I would love to try the script if there was a package for it. hmm


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#6 2016-02-25 14:08:05

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: m : finds and plays music [bash script]

ugjka wrote:

Umm sorry if that sounded rude, I meant that I would love to try the script if there was a package for it. hmm

You don't need a package for a simple bash script. If you want to test it, you can even store it in /tmp and just run it.

35: Why do you need a windowing toolkit?

whiptail is similar to dialog.
It seems there is an option to ask for a youtube search & download if there is no local result. I think I'd have used a commandline switch for that, but everyone has their own preferences.

58: You concatenate all search parameters into a single word.  Your example of "m radioh cr" would make searchterm radiohcr.  Is this what you want?

Well, the parameters are separated with a space, and then later the script loops over the space separated words. An array may be a better choice.

Last edited by progandy (2016-02-25 14:35:39)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#7 2016-02-25 14:22:00

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: m : finds and plays music [bash script]

Trilby wrote:

33: What is sentaku, I cannot find it anywhere in the repos or AUR

In his defense, he did at least provide a URL to sentaku github in the comments at the top.

@arnaudv6
Maybe change your error message to

Unable to find <binary> : <url of project>

Or such, at least for stuff that's not in AUR. Also, if any such message would exceed 80 characters, consider splitting to two lines.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#8 2016-02-25 14:25:24

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,794
Website

Re: m : finds and plays music [bash script]

progandy wrote:
ugjka wrote:

Umm sorry if that sounded rude, I meant that I would love to try the script if there was a package for it. hmm

You don't need a package for a simple bash script. If you want to test it, you can even store it in /tmp and just run it.

It has 4 dependencies (one of which is not even in the AUR), that's not simple anymore.


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#9 2016-02-25 14:33:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: m : finds and plays music [bash script]

alphaniner wrote:

In his defense, he did at least provide a URL to sentaku github in the comments at the top.

Ah, sorry - I missed that.  But as above, it does seem like a lot of not-particularly-common dependencies for a simple task.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2016-02-25 18:19:09

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

Woohoo ! Impressive feedback.
Thank you all much for your concern.

ugjka wrote:

It has 4 dependencies (one of which is not even in the AUR), that's not simple anymore.

I completely get your point. I can not easily provide a AUR package for I use gentoo,
but problem is the same with gentoo : no package.
I will edit my first post with a link to sentaku-0.3 you can place in ./local/bin (if it is in your $PATH)

Trilby wrote:

30: Why do you say "no tilde please", it'd work just fine with a tilde, just get rid of the quotes, and/or just use $HOME
38: This line does nothing useful
39: Don't do this: like line 30 comment, just do it right in the first place.  Do not try to reinvent the wheel.

My bash knowledge is bad here : so there would be a simple way where user can enter ~/Music with spaces folder and it all works ?

Trilby wrote:

36: Why do you need youtube-dl to play local music?  Have this test only under the conditional blocks that actually need youtube-dl
62-65: Why not just have this in the case statement on line 55 to save a variable and a conditional test:
    *) echo "${help}"; exit ;;

Right : thanks ! Corrected...

Trilby wrote:

86-91: Equivalent:
    find "${music_dir}" -printf "%P\n" | sort > "${index_file}"

I don't think so : with my way, find prints out relative paths, which cuts out 40% of index-file size in my case.

Trilby wrote:

~103: I don't get the logic.  You create a list of all files with find, then you grep that list for your match..............

Well that to me is the whole point of having an index: blazing fast first search, lessens disk accesses.
(I reboot my machine each day at least, but maybe I only once a month add a music to my collection. Plus I love fast smile )

Thank you much!

edit: had->add

Last edited by arnaudv6 (2017-05-25 11:51:01)

Offline

#11 2016-02-25 18:29:54

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

progandy wrote:

I think I'd have used a commandline switch for that, but everyone has their own preferences.

There is a -y switch which bypasses local search.
But when searching for a local file and finding none, the script proposes searching youtube smile

progandy wrote:

Well, the parameters are separated with a space, and then later the script loops over the space separated words. An array may be a better choice.

Genuinly candide here : would it be better ?

Offline

#12 2016-02-25 18:31:42

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

alphaniner wrote:
Trilby wrote:

33: What is sentaku, I cannot find it anywhere in the repos or AUR

In his defense, he did at least provide a URL to sentaku github in the comments at the top.

@arnaudv6
Maybe change your error message to

Unable to find <binary> : <url of project>

Or such, at least for stuff that's not in AUR. Also, if any such message would exceed 80 characters, consider splitting to two lines.

Right thanks ! Corrected

Offline

#13 2016-02-25 18:48:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: m : finds and plays music [bash script]

arnaudv6 wrote:
Trilby wrote:

86-91: Equivalent:
    find "${music_dir}" -printf "%P\n" | sort > "${index_file}"

I don't think so : with my way, find prints out relative paths, which cuts out 40% of index-file size in my case.

Did you try my version?  The %P in the printf (not %p) prints relative paths.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#14 2016-02-25 18:53:41

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

Ok now youtube-dl is optional and checked at runtime (thanks @Trilby)
I am considering dropping whiptail for a simpler solution
I just wish to now @Trilby @ugjka @progandy: whiptail is not installed on your systems ? Not so common then ?

When it comes to sentaku, I just don't get why this script is not more used !? I can think of a few alternatives:

But sentaku being one-file bash script made it a go for m

Would this help if I dynamically launched whichever of mpv, mplayer or gst-play is installed ?
or have player be a config option ?

edit : "help to" -> "help if I", lauch -> launched

Last edited by arnaudv6 (2017-05-25 11:52:03)

Offline

#15 2016-02-25 18:59:31

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

Trilby wrote:

Did you try my version?  The %P in the printf (not %p) prints relative paths.

You're right I did not neutral
And you're right again : your way is shorter and more elegant : corrected, thanks !

Offline

#16 2016-02-25 19:14:27

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: m : finds and plays music [bash script]

I would suggest adding '-mindepth 1' to to Trilby's find command to avoid a blank line. Alternately, '-type f' will avoid printing directories - which I think you'd want to do - and consequentially 'bypass' the blank line problem. Remember, a particularly great thing about shell scripting is that you can test stuff from the interactive prompt:

$ music_dir=~'/Music MP3'
$ find "${music_dir}" -mindepth 1 -printf "%P\n" | sort
...
$ find "${music_dir}" -type f -printf "%P\n" | sort
...

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#17 2016-02-25 20:17:13

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

Thanks for the support, guys.
I removed whiptail dependency.

@alphaniner : I added the -mindepth 1 switch, thanks.
as a matter of fact, I like to have directories listed, so I can play one CD/artist at a time.

Offline

#18 2016-02-26 12:16:15

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

m now gets sentaku from github and puts it in its directory if not found.
Only mpv is required so.
I will soon advertize its md5sum. Thanks guys for all your insights and comments.

Offline

#19 2016-02-26 12:38:51

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,794
Website

Re: m : finds and plays music [bash script]

It works, nice smile


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#20 2016-02-26 13:36:47

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: m : finds and plays music [bash script]

Moving to "Community Contributions".

Edit: @arnaudv6, please give a better title to the thread. Perhaps a name for your script.

Last edited by x33a (2016-02-26 13:39:10)

Offline

#21 2016-02-26 13:38:12

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

ugjka wrote:

It works, nice smile

Thanks! smile

Offline

#22 2016-02-26 16:01:21

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

x33a wrote:

Moving to "Community Contributions".

Edit: @arnaudv6, please give a better title to the thread. Perhaps a name for your script.

Flattered, x33a.

I kind of like m's name: obviously short and just one letter to type.
I guess potentially harmful programs (like rm) need names with at least 2 distant letters on the keyboard to limit mistakes though.

Changing the title of the thread.

Offline

#23 2016-06-04 08:25:58

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

Trilby wrote:

30: Why do you say "no tilde please", it'd work just fine with a tilde, just get rid of the quotes, and/or just use $HOME

38: This line does nothing useful

39: Don't do this: like line 30 comment, just do it right in the first place.  Do not try to reinvent the wheel.

I did some cleaning, and finally applied your advises, Trilby:
I don't know why I delayed so much, anyway thank you much for ginving it in the first place.

Also I would like to sort directories first in my files index, but keep the exact same format:

find "${music_dir}" -mindepth 1 -printf "%P\n" | sort > "${m_index_f}"

I tried such solutions with no success, does anyone have a better way please?

tree -fin --noreport -o music_index.2.txt
for ((l = 0; l <= 24; l++)); do find . -mindepth ${l} -maxdepth ${l} -type f | sort; done > list4
for ((l = 0; l <= 24; l++)); do find . -mindepth ${l} -maxdepth ${l} | sort; done > list4

Last edited by arnaudv6 (2016-06-04 08:26:22)

Offline

#24 2016-06-06 11:24:14

chickenPie4tea
Member
Registered: 2012-08-21
Posts: 309

Re: m : finds and plays music [bash script]

I downloaded the script and ran it with the -u switch for it to create a database
I just got an error message
"/home/dka/bin/m: line 99: /home/dka/music MP3/music_index.txt: No such file or directory
find: ‘/home/dka/music MP3’: No such file or directory"

Shouldn't the script just create the directory itself?
like
mkdir -p $HOME/music/MP3

one other thing I typed in a search and got
"No file matches. Get first youtube result? (Y/n)"

but before I typed anything it started downloading form youtube.
edit, ok I found the line and increased the time from 4 to 8 seconds

Forgot to say I like it! it works quickly. I think the title of the thread should be
"m: finds plays and downloads music"

Last edited by chickenPie4tea (2016-06-06 15:26:25)


You can like linux without becoming a fanatic!

Offline

#25 2016-06-07 17:39:19

arnaudv6
Member
Registered: 2016-02-25
Posts: 73

Re: m : finds and plays music [bash script]

chickenPie4tea wrote:

I downloaded the script and ran it with the -u switch for it to create a database
I just got an error message
"/home/dka/bin/m: line 99: /home/dka/music MP3/music_index.txt: No such file or directory
find: ‘/home/dka/music MP3’: No such file or directory"

Well, I reacted too late: you must know by now, music dir was set in a variable for the user to adapt.
I changed m so it complies with XDG user directories now: does this suit you best?

When it comes to youtube downloads behaviour, here are my motives:

  • I often leave the console without looking at it:
    m then finds and plays something with no more input from I.

  • Delay seems fine to me but I am ready to hear about your experience.

Thanks for trying m!

Last edited by arnaudv6 (2016-06-18 06:27:32)

Offline

Board footer

Powered by FluxBB