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:
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? – Nathaniel M. Beaver Jul 16 '20 at 01:56