5

I would like to remove an MP3 file that is currently being played back in mplayer. If possible I would like to do it with an mplayer command. So that I don't have to quit mplayer and delete this file with rm or open another terminal and delete the file there.

2
  • it is theoretically possible in most cases, but it is unlikely that the OS will allow it. have you tried it, and if so what results/errors did you encounter? Oct 21, 2013 at 17:00
  • Yes, you can "delete" files while they are open. This will not actually delete the file nor remove the space used until the last application having an open handle to that file closes it. The file will disappear from your directly listing though. (or it is it a hardlink the file will disappear from the directory listing and the usage count will be decreaed by one. When/if it reaches 0 and no handles are open it will still be deleted).
    – Hennes
    Jan 23, 2017 at 18:17

6 Answers 6

4

Yes, possible and easy. I use a modified version of the following to curate/prune my media collection.

(1) Save the following bash script (rmplaying.sh)

#/bin/bash
# rmplaying.sh
# only works when exactly 1 instance of mplayer is running
function playing {
PID=`pidof mplayer`
WHICH=`which mplayer`
if [ $PID ]; then
    lsof -p $PID | awk '{ if ($5=="REG" && $4!="mem" && $9)print $0 }' | grep -v "$WHICH" | grep -oP '\/.*'
fi
}

FILE="$(playing)"
echo "file: $FILE"
if [ "$FILE" ]; then
  rm "$FILE" && echo "Removed '$FILE'"
fi

(2) Set permissions to allow it to execute

chmod +x rmplaying.sh

(3) Edit or create the file "~/.mplayer/input.conf" and add the line

Ctrl+d run "/path/to/rmplaying.sh"

That's it.

To test, use mplayer to play a file and press "Ctrl+d". The file will be removed via the "rm" command, but the media will continue to play.

If you started mplayer from a terminal window, you should see the message "Removed '/path/to/media/file.mp3'". If you didn't launch it using terminal, check that the file was removed from its directory.

*Move the playing() function to its own utility script for further customization

3

You cannot delete a file. The only thing a user can do is to call unlink(2) (e.g. using rm(1)) which removes the file name from the directory which results in a decremented hardlink counter. Only when the hard link counter reaches zero and no application has an open file handle of this file, then the operating system will delete the file.

Since you play the file in mplayer there will be an open file handle and the file cannot be deleted. The unlink(2) system call should be successfull, nevertheless. In practice this means you can call rm(1) on the file, but don't expect to have free'ed up the space until you quit mplayer.

1
  • My wish is not to delete the file straight away. I can wait until the time I quit mplayer. I'm looking for a convenient way to tell the file system, "remove this file when you can". Preferably from mplayer itself. So I don't have to fire up another terminal, look for the file name and delete it explicitly with the rm command.
    – mabalenk
    Oct 21, 2013 at 19:58
2

To solve this, I have added the following line to the file ~/.mplayer/input.conf:

DEL run "rm '${path}'"

This makes the delete key in mplayer run the shell command rm on the complete path of the file mplayer is currently playing.

Note that the ${path} part is substituted by mplayer itself: ${path} is not a shell variable interpolation.

UNFORTUNATELY, though, the fact that mplayer insists on using the shell for running the command means that a carefully crafted filename to delete might lead to EXECUTION OF ARBITRARY SHELL CODE. Ideas for avoiding that are welcome.

Note also that the single quotes are important to make this work with spaces in the path of the file, but stops it working for files with single quotes in their names.

Note also that this does not require shell scripts or fiddling around with lsof, and thus should run on any unix-y operating system, and work with multiple instances of mplayer running concurrently.

The exact command documentation can be found in mplayer's slave mode documentation.

2
  • Beware of losing precious files by accidentally hitting the delete key (it actually happens). Better to move to a trash folder using a script and a different key binding as in @lkchoi's answer.
    – rfabbri
    Jul 1, 2020 at 13:29
  • In that case, I usually pause mpv, look up its PID, ls -l /proc/PID/fd/ to look up the FD of the (deleted) file, and then I cat /proc/PID/fd/FD > restored-file. Not nice, but enough for me for those rare cases.
    – ndim
    Jul 2, 2020 at 14:23
0

lkchoi and ndim solutions didn't worked for me.

So I create a system which I call "Cesar", if the songs is bad, I remove it, if it's good move to another folder.

This is useful when selecting audios, for example that WhatApp backup, which is an important recording?

Filtering songs in a huge Hair Metal package I also used, it's simple:

mplayer */*/*3 | sed "s@Playing \(.*\).\$@\nrm \"\1\"\nmv \"\1\" ../saved\n@"

mplayer echos "Playing" + song name + file extension + a dumb dot, I removed those and mounted the statement needed to remove or move.

It will echo all expected info, but instead showing Playing... it will show:

rm "Absolute Hair Metal Vol. 56-111/Absolute Hair Metal Vol. 100/06 Leatherwolf - The Calling.mp3" mv "Absolute Hair Metal Vol. 56-111/Absolute Hair Metal Vol. 100/06 Leatherwolf - The Calling.mp3" ../saved

...

I know it's annoying to click twice then middle mouse to paste and then hit enter. but others solutions stopped working and there's also some security issues.

Maybe you'll need to change the \n for \r. resulting:

mplayer */*/*3 | sed "s@Playing \(.*\).\$@\rrm \"\1\"\rmv \"\1\" ../saved\n@"
-1

First of all, why don't you just try it and see? Anyway, deleting from mplayer itself is probably impossible, why would a media player have file manipulation abilities? It's like asking if you can copy files using acroread.

Deleting a file while it is being played should not be a problem. The file will be copied into RAM as soon as a program opens it and you can delete the file from your hard disk with no problem (just checked this using mplayer). Windows doesn't like you doing that if I remember correctly but I have never encountered this behavior in Linux.

4
  • The file is not copied to RAM. It stays on the same medium until the last reference to it (including open file descriptors) ceases to exist. (Of course some parts of the file are cached in RAM, but that has nothing to do with the unlink operation.) Oct 21, 2013 at 19:09
  • @DavidFoerster it is not copied to RAM while being reproduced? I am not saying it will be removed from the file system but it should be loaded into RAM when you access it.
    – terdon
    Oct 21, 2013 at 19:13
  • @terdon The OS will surely cache parts of the file in RAM. But mplayer will not load the file in RAM, otherwise it would consume huge amounts of memory when watching videos and the video size has to be smaller than the RAM size.
    – Marco
    Oct 21, 2013 at 19:16
  • Yes, usually the OS will at least partially cache the file in RAM, but it will not delete the original until all references to it are gone. Oct 21, 2013 at 19:16
-2

I do not expect this to be possible. While I haven't used mplayer myself, I think I ran into a similar situation with Totem. Basically, since an application is already reading/accessing the file, you cannot delete it. At the very least, you'll need to stop playing the file before you can delete it, whether that happens from within mplayer or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .