1

My question: I'd like to create a desktop entry in (kde dolphins servicemenu) to convert audio/video files.

I have tried it with the follwing:

[Desktop Entry]
Actions=Convertwav2Mp3
Icon=audio-x-flac
MimeType=audio/*
ServiceTypes=KonqPopupMenu/Plugin
Type=Service
X-KDE-Priority=TopLevel

[Desktop Action Convertwav2Mp3]
Exec=ffmpeg -i %f -codec:a libmp3lame -b:a 320k .out.mp3 && mv .out.mp3 %f
Icon=audio-x-flac
Name=Convertwav2Mp3

Works like a charm when put in /usr/share/kservices5/ServiceMenus/.

BUT: The mv command renames the output file to the original filename with its file extension (which is wav but should be mp3).

How can i change the command as to rename it from file.wav to file.mp3 within the Exec field?

1 Answer 1

0

Option #1: append the .mp3

Exec=ffmpeg -i %f -codec:a libmp3lame -b:a 320k %f.mp3

This will result in e.g. converting

my_file.wav

to

my_file.wav.mp3

which may not be desirable.

Fun fact: there was used to be the %n key that gave the base name like this:

Exec=ffmpeg -i %f -codec:a libmp3lame -b:a 320k %n.mp3

The FreeDesktop spec has listed this as deprecated from version 1.0:

Deprecated Exec field codes: ... %n (the base name of a file) ...

https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html

This may have worked in older versions of Dolphin, but in the version I used (17.12.3), the %n key does the same as the %f key, so this will not work as desired.

Option #2: use bash parameter expansion

To get this instead:

my_file.mp3

we will need to remove the file extension. This can be accomplished using bash parameter expansion:

Exec=bash -c 'wavfile='\''%f'\''; mp3file="${wavfile%.wav}.mp3"; ffmpeg -i '\''%f'\'' -codec:a libmp3lame -b:a 320k "$mp3file"'

We have to invoke bash explicitly because the Exec key is passed through /bin/sh, which does not support this syntax.

Caveats

Both of these options will work with filenames that have spaces. However, they will not work as expected in other cases, such as:

  • If the MP3 filename already exists, it will silently fail.

  • If the WAV filename contains a parameter expansion string such as $0 or $USER it will silently fail.

  • If the WAV filename contains a command substitution string such as `date` or $(date) it will silently fail.

  • If the WAV filename is e.g. example.WAV instead of example.wav, it will result in example.WAV.mp3.

To make these problems more tractable and easier to debug, I would recommend writing a separate shell script and invoking it directly; there are many examples:

Further comments

As an aside, you may already know this, but the desktop file can be copied to

~/.local/share/kservices5/ServiceMenus/

instead of

/usr/share/kservices5/ServiceMenus/

which is useful if you want to install it for a single user or don't have root privileges.

Finally, I would recommend using

MimeType=audio/x-wav

instead of

MimeType=audio/*

since this only works on WAV files.

Related questions:

2
  • Thanks for the reply but the problem is specifically about the correct renaming without the doubled file extensions....The command itself already works fine...
    – user634093
    Jul 15, 2020 at 20:09
  • The Exec field does not have any facilities for manipulating filenames. Your current example uses the shell to call ffmpeg and run multiple commands, but it could just as easily use a Python script, compiled C program, etc. Could you explain your requirements in more detail? Jul 16, 2020 at 1:56

You must log in to answer this question.

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