I want to be able to save the name of an .mp3 file every time I open a song with VLC.

Edit: As you guys in the comments mentioned;

  1. I want to open VLC directly in a player window.
  2. I want to save the name of the song that is being played into a text file.

How can I achieve this goal? Thanks in advance.

share|improve this question
2  
You would have to write a script to do what you want and then force every user to use the script rather then launching VLC direct. Your question is otherwise lacking details so .... – Panther Jun 5 '17 at 18:07
    
@bodhi.zazen no I want to launch VLC (or any other program) directly and be able to run a script automatically. I'll add more details to the question. – Amir A. Shabani Jun 5 '17 at 18:09
1  
Well you would have to either write a script or modify the source code and recompile the apps you want. – Panther Jun 5 '17 at 18:11
1  
@JacobVlijm Yes, sorry if it wasn't clear, I'll edit in a sec. – Amir A. Shabani Jun 5 '17 at 18:19
1  
VLC tracks recent files see and superuser.com/questions/287137/…. – Panther Jun 5 '17 at 18:28
up vote 2 down vote accepted

You can get the currently playing track name (Address) using:

lsof -p `pidof -s vlc` | grep -o "/.*\.mp3"

If you want to save the name when you double click on a mp3 file to open VLC here is my Idea:

open VLC's desktop file:

sudo nano /usr/share/applications/vlc.desktop

then edit Exec line to make it look like:

Exec=bash -c "{ /usr/bin/vlc --started-from-file %U; }& sleep 1; lsof -p `pidof -s vlc` | grep -o "/.*\.mp3" > /home/user/list"

it's going to save the file name at /home/user/list.

Otherwise I suggest you to create a simple keybinding for sh -c 'lsof -p $(pidof -s vlc) | grep -o "/.*\.mp3"' so whenever you press that keys it saves the currently playing song's name.

We can also create a script and run it at the same time with VLC, and keep it running in the background then withing that script we can check which track is being played right now.

share|improve this answer
    
The line: lsof -p `pidof -s vlc` | grep -o "/home.*mp3" gives me this error: lsof: WARNING: can't stat() tracefs file system /sys/kernel/debug/tracing Output information may be incomplete. – Amir A. Shabani Jun 5 '17 at 19:00
    
Warning doesn't related to the command syntax, Here:1, Here:2. Just make sure VLC is running and is playing a song then run that command again, I also updated the command, use this one instead the old one. you can test it using sudo too. – Ravexina Jun 5 '17 at 19:10

Automatically log all your played songs, with a time stamp

Alternatively, without changing anything to your .desktop file or having to manually log your songs, you can run the background script below. It will create a log file, named played_songs, in your home directory:

enter image description here

Advantages are:

  • Fully automatically logs all your played songs
  • No need to change your .desktopfile, which means it also works when you start VLC from the command line.

Disadvantages are:

  • Although I couldn't measure or see any additional processor load, theoretically it adds some activity. This is practically none though, even on stone-age boxes.
  • From time to (long) time, you will need to "empty" or remove the log file, since (currently) it has no limit in size. This can be easily fixed though if you like.

The script

#!/usr/bin/env python3
import subprocess
import os
import time

home = os.environ["HOME"]
log = os.path.join(home, "played_songs")
def_name = "VLC media player"

def get(cmd):
    try:
        return subprocess.check_output(cmd).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass

song1 = None

while True:
    time.sleep(2)
    pid = get(["pgrep", "vlc"])
    if not pid:
        song = None
    else:
        # only do anything at all if vlc runs
        wins = get(["wmctrl", "-lp"])
        if wins:
            wins = wins.splitlines()
            try:
                # check if any of the windows belongs to vlc
                # this will not if it is closed in the meantime
                match = [w.split()[0] for w in wins if pid in w][0]
            except IndexError:
                pass
            else:
                # ...if so, retrieve the song name, only log if there is a new song
                song2 = get(["xdotool", "getwindowname", match])
                if all([song2, song2 != song1, song2 != def_name]):
                    open(log, "a+").write(
                        time.strftime("%H:%M:%S_%d-%m-%Y")+"\n   "+\
                        song2.replace(def_name, "").rstrip(" - ")+"\n"
                        )
                song1 = song2

How to use

  1. The script needs both xdotool and wmctrl:

    sudo apt-get install xdotool wmctrl
    
  2. Copy the script into an empty file, save it as log_vlc.py

  3. Test- run it from a terminal by the command:

    python3 /path/to/log_vlc.py
    

    Open any song in VLC in either way (cli, gui), the log file should be created and show the logged songs.

  4. If all works fine, add ity to Startup Applications: Dash > Startup Applications > Add. Add the command:

    python3 /path/to/log_vlc.py
    

Explanation

Once per two seconds, the script:

  • checks if VLC is running at all, does nothing if not

    pid = get(["pgrep", "vlc"])
    if pid:
        # only do anything at all if vlc runs
    
  • if VLC is running, it finds its window and parses out the song title

    wins = get(["wmctrl", "-lp"])
    if wins:
        wins = wins.splitlines()
        try:
            # check if any of the windows belongs to vlc
            # this will not be the case if it is closed in the meantime
            match = [w.split()[0] for w in wins if pid in w][0]
        except IndexError:
            pass
        else:
            # ...if so, retrieve the song name, only log if there is a new song
            song2 = get(["xdotool", "getwindowname", match])
    
  • (only) if the song title changes, obviously a new song is played, then the song name is logged into the log file, time stamped.

            if all([song2, song2 != song1, song2 != def_name]):
                open(log, "a+").write(
                    time.strftime("%H:%M:%S_%d-%m-%Y")+"\n   "+\
                    song2.replace(def_name, "").rstrip(" - ")+"\n"
                    )
                print(song2)
            song1 = song2
    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.