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:

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:

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 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:

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).

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).

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).

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: