Getting your MTP devices working on Linux
Music Library
Just because the MTP protocol is promoted by Microsoft doesn't mean you can't get music onto your MTP devices.
If your day begins like mine, the first computing device you encounter is a music player. In the good old days, you could pick up any player with USB connectivity and mount it easily on Linux. Although some players still provide this instant Linux connectivity, increasingly, many of the most popular brands are moving towards the Microsoft-promoted Media Transfer Protocol (MTP).
MTP offers more controlled access to media files, presumably to minimize data corruption. MTP devices (including MP3 players as well as the latest generation of music-playing mobile phones) are not exposed as mass storage devices; getting your music onto these devices requires a little effort – and a little library called libmtp.
The libmtp library [1] is an open source project designed to bring MTP support to Linux and Unix-based systems. As you will learn later in this article, several open source music applications offer MTP support through plugins developed around the libmtp library. If you are using a music app that doesn't work with MTP, or if you would like to create your own custom tools for interfacing with MTP devices, just download and build the latest version of libmtp. The library also includes a collection of sample programs that serve as a simple command-line interface for interacting with MTP devices.
Get libmtp On Board
Although libmtp might be available in your distro's repository, I would suggest you grab the latest version from the project's website [2] because it isn't too difficult to install from source.
The MTP protocol needs the USB transport protocol, which means that the libmtp library needs the libusb library. It's safe to get libusb from your distro's repository, along with its development package (either libusb-dev or libusb-devel).
With the transport library in place, you will now want to download the latest libmtp library (v0.3.6 at the time of writing) and install it as follows:
$ tar xvf libmtp-0.3.6.tar.gz-C /usr/local/ $ cd /usr/local/libmtp-0.3.6 $ ./configure $ make $ su -c "make install" OR $ sudo make install
The libmtp package installs the binaries under /usr/local/bin and its shared libraries under /usr/local/lib. Because this isn't a standard library path on many distributions, you can add it to your /etc/ld.so.conf file by opening the file in your favorite text editor and adding the line include /usr/local/lib/. When you've done this, save the file, then run ldconfig as root to update the links. Finally, check to see that everything is in order:
$ whereis mtp-detect mtp-detect: /usr/local/bin/mtp-detect $ ldd /usr/local/bin/mtp-detect linux-gate.so.1 => (0xb7f79000) libmtp.so.8 => /usr/local/lib/libmtp.so.8 (0xb7f3c000) libusb-0.1.so.4 => /lib/libusb-0.1.so.4 (0xb7f34000) libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7dd8000) /lib/ld-linux.so.2 (0xb7f7a000)
Query Your Device
The libmtp package is actually a C library for writing programs to query an MTP device. Also, libmtp comes with a set of example programs that will do nearly everything you would want to do with your MTP device. The source code for these programs can be found under the examples/ directory in the same place that you untarred the source package (/usr/local/libmtp-0.3.6/examples, in this case).
One example program that's pretty useful for testing to see whether your device works with the library is mtp-detect. If you connect your device to the USB port, power it on, and wait a few seconds to let it settle down, you can then probe it with mtp-detect (Listing 1).
Listing 1
Probing with mtp-detect
Listing 1 is just a brief snippet of the output produced by mtp-detect, which not only detects the device but also lists the types of files the device can handle, as well as other attributes like the battery level.
On the other hand, if you get a No raw devices found message, either your device doesn't support MTP (and is probably already mounted as a mass storage device) or it isn't connected properly. To see whether your device is connected, look for it with lsusb:
$ lsusb Bus 004 Device 029: ID 10d6:1101 Actions Semiconductor Co., Ltd D-Wave 2GB MP4 Player / AK1025 MP3/MP4 Player
Exploring the Examples
With the example programs bundled with libmtp, you can do a lot more than just detect your device. The mtp-files program, for instance, lists information on the files already on the device (Listing 2).
Listing 2
Listing Files with mtp-files
mtp-files produces a flat view of all the files on your device, along with attributes such as the File ID and Storage ID, which you will need to perform actions on these files. Also, you can get a list of all the albums on the device with mtp-albums, which gets the Album ID of each album and the number of tracks on the album.
Similarly, mtp-tracks lists all the files, along with their Track ID and original file name (Listing 3).
Listing 3
mtp-tracks Output
If you play both audio and video, you can list them with the mtp-folders program:
$ mtp-folders Friendly name: superk 83886081 Music 83886082 The Adored 83886083 New Language 83886086 All the Time 83886110 Video 83886111 Bitter Sweet 83886112 CSS
To track down a song and see whether it exists on the device, you can use the Track ID number with the mtp-trexist program:
$ mtp-trexist 33554454 libmtp version: 0.3.6 Yes OK.
Now that you know the track exists, you can use the mtp-getfile program to move it off the player and onto your hard drive. mtp-getfile needs two arguments: the File ID (or Track ID) of the track and the location where you want to copy the file:
$ mtp-getfile 33554454 The-Race.wmv
This command will grab the file with the File ID 33554454 and save it as The-Race.wmv in the current directory.
The mtp-sendtr program lets you send a track to the device. In addition to the name of the file, this program also asks for lots of metadata information typically associated with a music file, such as title, artist, album, and genre (Listing 4).
Listing 4
Sending a Track
If you don't want to enter so much information for every track, just specify the ones you need at the command line with the appropriate switch and round off with the -q switch, which asks the program to ignore the missing information. For example:
$ mtp-sendtr -q -t SomeTitle -a SomeArtist -l SomeAlbum Music/song.mp3 on-the-player.mp3
To delete a file, simply specify its File ID or Track ID with the mtp-delfile program:
$ mtp-delfile -n 33554451
In my opinion, the most useful program is mtp-newplaylist,
$ mtp-newplaylist -i 16777249 -i 16777247 -i 16777224 -n briansetzer
which arranges music files into a playlist. Once the program creates the playlist, it displays the Playlist ID; mtp-playlists lists all the playlists on the device.
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.