LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-10-2015, 03:47 PM   #1
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Rep: Reputation: 31
How can i extract currently played file from mplayer


So here's the scenario. I like to play my music through the cmd line with mplayer:

Code:
mplayer -shuffle Music/path/*.mp3
I've been using it exclusively to play my audio files and I like it because its bring me back into the cmd line which was my initial intention. I use regex to filter the genre or range of songs. What i would like to do is make some sort of script that will find the absolute path of the currently playing song and copy that file to a playlist of my choice.

Example:

1. music is playing and I like the current selection
2. type a cmd (from a different shell obviously) to lock in on that file
3. copy the file to a new location (file) specified by the argument given

Code:
./getsong playlist=Jazz
The above script would get the song currently playing and put it in a directory named Jazz appending to the file if it exists or creating it if it doesn't.

This is what I want in theory but not sure how to implement it. I can do it manually by looking at the data, browsing to the location, and copying it to another location. But where do I start in writing a script for it?

I listed the processes running and filtered mplayer but it seems to show all the music that will play and not just the single song. So where would I navigate in order to find that single item?

I am not stuck on mplayer I am willing to use another cmd line application but I think they would work in the same context.
 
Old 08-10-2015, 07:37 PM   #2
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Original Poster
Rep: Reputation: 31
So far I've been able to locate the currently playing file with the below cmd

Code:
lsof -d 3 | grep mplayer >> /path/to/file.txt
This spits out a text file which I can reference and use as a playlist but it needs further parsing to get only the field containing the path of the file.

Code:
mplayer   1368 dman    3r   REG     8,20 13243890 28183428 /home/dman/Music/folder/of/currentsong.mp3
I'm thinking awk would be the next choice in order to accomplish this.
 
Old 08-10-2015, 10:09 PM   #3
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374

Rep: Reputation: 50
If you are not stuck on mplayer, I use mpd to play music. The mpc cli client lets you query the mpd status and much more.
 
Old 08-10-2015, 10:19 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,133

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
And if you are stuck on mplayer, no point shelling out to grep then awk - do it all in awk.
 
Old 08-12-2015, 09:01 AM   #5
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Original Poster
Rep: Reputation: 31
Made some progress

My code hasn't grown much but I'm still working on it.

This is the begining of a script that I want to use. I set it as executable and put it in my users PATH for convenience. Currently it will append the current track to a designated file that I can source as a playlist for mplayer later.

Code:
lsof -d 3 | grep mplayer | cut -c 70- >> /home/dman/Music/list.txt
But to add fine tuning I would like to designate a filename after the program name in order to catagorize these lists.

example: program_name variable_name

Where variable would be a file with the name I specify after the program_name

Code:
getit Jazz
In the above the program (getit) would look for a name (Jazz) and append to a list or create it if it doesnt exist. Thats what I would like it to do but not sure how.

Is this where I would try and insert a variable/variables?
Would an if statement be what I am looking for?

I use the following in a dedicated always on top slim terminal window to display the name of the currently played selection and it changes as the song plays. Its a little crude but it works.

Code:
watch -d 'lsof -d 3 | grep mplayer | cut -c 70-'
I am a self taught Linux user probably most people are. I say that to say this. I have no structure to follow in my journey of Linux knowledge. I just let the breeze take me in any direction that fits according to the given situation. Randomly searching topics as they come to mind. Which causes conflict when I get to a point where I should have learned something and moved onto something else instead. I'm all over the place but am definitely enjoying the ride.
 
Old 08-12-2015, 09:14 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,133

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
I don't like cut sometimes as the "fields" aren't natural - you have to collapse the multiple whitespace to one. And using a char count like that is fragile. Try this
Code:
lsof -d 3 | awk '/mplayer/ {print $NF}'
That will always print the last field on a line containing "mplayer". You could use $9 rather than $NF if you are worried about another field appearing sometime.
 
Old 08-12-2015, 09:46 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,735

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
Many ways to accomplish the same task. An if-then statement would work but I would use a case statement.


http://www.bashguru.com/2009/11/how-...ll-script.html
http://www.tldp.org/LDP/abs/html/
 
Old 08-12-2015, 03:50 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
it's been a while since i used mplayer (these days i use mpv for video and moc for audio) - but i distinctly remember that mplayer had some kind of "remote" mode where you could query it while it's playing.

so...
Code:
man mplayer
(you can search with '/<searchterm>')
 
Old 08-12-2015, 09:52 PM   #9
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by syg00 View Post
I don't like cut sometimes as the "fields" aren't natural - you have to collapse the multiple whitespace to one. And using a char count like that is fragile. Try this
Code:
lsof -d 3 | awk '/mplayer/ {print $NF}'
That will always print the last field on a line containing "mplayer". You could use $9 rather than $NF if you are worried about another field appearing sometime.
Thanks for the reply but this code as it stands doesn't function as well as mine with 'cut'. I'm sure with some more tweaking it would do the trick but how my original works is it starts from 1st field and subtracts to the beginning of the last field exactly where I want it. Since the folder is constant the data stays the same and works everytime (so far). With your code it stops at the 1st blank space leaving out the rest of the path.

Quote:
Originally Posted by michaelk View Post
Many ways to accomplish the same task. An if-then statement would work but I would use a case statement.


http://www.bashguru.com/2009/11/how-...ll-script.html
http://www.tldp.org/LDP/abs/html/
tldp.org is great and I will continue to reference that. Thank you. Time for some deep reading and understanding

Quote:
Originally Posted by ondoho View Post
it's been a while since i used mplayer (these days i use mpv for video and moc for audio) - but i distinctly remember that mplayer had some kind of "remote" mode where you could query it while it's playing.

so...
Code:
man mplayer
(you can search with '/<searchterm>')
Was unable to find that query/ search option but maybe it was overlooked.
 
Old 08-13-2015, 01:12 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,133

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Sorry about that - I guess you have blanks in the name. Yes it can be worked around, but not worth the effort.
Forget I said anything ....
 
Old 08-15-2015, 12:44 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Dman58 View Post
Was unable to find that query/ search option but maybe it was overlooked.
http://www.mplayerhq.hu/DOCS/HTML/en/control.html
http://www.mplayerhq.hu/DOCS/tech/slave.txt

this should get you going.

fwiw, i gave up on mplayer as soon as mpv was good enough.
 
Old 08-19-2015, 06:55 PM   #12
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Original Poster
Rep: Reputation: 31
Few days and lots of reading later I have officially made a functional program that meets my needs as of now. I will gradually add more code to it to customize the features later.

Code:
#!/bin/bash

#Move current playing file to playlist specified

echo What playlist do you wanna save this song to?:\n

read playlist

if [ -f "/home/dman/Music/list/${playlist}.txt" ]; then

echo "Playlist $playlist exists."; # add command here

lsof -d 3 | grep mplayer | cut -c 70- >> /home/dman/Music/list/${playlist}.txt

else

echo "Playlist $playlist not found."; touch /home/dman/Music/list/${playlist}.txt

echo "Playlist $playlist created"

lsof -d 3 | grep mplayer | cut -c 70- >> /home/dman/Music/list/${playlist}.txt

fi; echo "Song added to "$playlist" see you next time."

exit


Thanks for all the suggestions.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File Manager keep track of played media grmrgecko Linux - Software 9 07-04-2015 12:35 AM
convert flv file to be played in XP ufmale Linux - Newbie 1 09-13-2008 08:13 PM
Played with MPlayer controls and now I'm in trouble gregorian Linux - Software 23 01-26-2007 08:05 AM
One avi file not getting played my mplayer. ShaanAli Linux - Software 3 05-01-2006 08:24 PM
extract a file for mplayer ? harys Linux - Software 1 04-04-2006 03:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration