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?
Option #1: append the .mp3
This will result in e.g. converting
to
which may not be desirable.
Fun fact: there was used to be the
%n
key that gave the base name like this:The FreeDesktop spec has listed this as deprecated from version 1.0:
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:
we will need to remove the file extension. This can be accomplished using bash parameter expansion:
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 ofexample.wav
, it will result inexample.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:
How to convert a m4a sound file to mp3?
How can I convert audio from ogg to mp3?
bulk/batch convert mp3 files to ogg via command line?
Converting mp4 to mp3
convert a folder of mp4's to mp3's with ffmpeg?
Further comments
As an aside, you may already know this, but the desktop file can be copied to
instead of
which is useful if you want to install it for a single user or don't have root privileges.
Finally, I would recommend using
instead of
since this only works on WAV files.
Related questions: