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.

By Mayank Sharma

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
01 $ mtp-detect
02 libmtp version: 0.3.6
03
04 Listing raw device(s)
05    Found 1 device(s):
06    Microsoft: Zune (045e:0710) @ bus 0, dev 27
07 Attempting to connect device(s)
08
09 MTP-specific device properties:
10    Friendly name: superk
11    Synchronization partner: {64DAA456-CB9A-4FED-905C-6674746D2C1C}
12    Battery level 93 of 100 (93%)
13 libmtp supported (playable) filetypes:
14    ISO MPEG-1 Audio Layer 3
15    Microsoft Windows Media Audio
16    Microsoft Advanced Systems Format
17    MPEG-4 Part 14 Container Format (Audio Empahsis)
18    Advanced Audio Coding (AAC)/MPEG-2 Part 7/MPEG-4 Part 3
19    Microsoft Windows Media Video
20    JPEG file
21    MediaCard
22    Firmware file

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
01 $ mtp-files
02 libmtp version: 0.3.6
03
04 mtp-files: Successfully connected
05 Listing File Information on Device with name: superk
06
07 File ID: 16777241
08  Filename: Austin Powers - Beautiful Stranger.mp3
09  File size 4190424 (0x00000000003FF0D8) bytes
10  Parent ID: 83886127
11  Storage ID: 0x00010001
12  Filetype: ISO MPEG-1 Audio Layer 3
13 File ID: 33554439
14  Filename: Fruit Bats - The Wind.wmv
15  File size 17479310 (0x00000000010AB68E) bytes
16  Parent ID: 83886110
17  Storage ID: 0x00010001
18  Filetype: Microsoft Windows Media Video
19 File ID: 50331711
20  Filename: OCT2008.jpg
21  File size 74353 (0x0000000000012271) bytes
22  Parent ID: 83886516
23  Storage ID: 0x00010001
24  Filetype: JPEG file

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
01 Track ID: 33554454
02  Title: The Race
03  Origfilename: The Race.wmv
04  Track number: 0
05  Duration: 486644  milliseconds
06  File size 45808803 bytes
07  Filetype: Microsoft Windows Media Video
08 OK.

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
01 $ mtp-sendtr Source-clip.mp3 on-the-player-clip.mp3
02 libmtp version: 0.3.6
03 Sending track Source-clip.mp3 to on-the-player-clip.mp3
04 type:mp3,1
05 Title> The best song there is
06 Album> Goodness Me
07 Album artist> The Best Ponga
08 Artist> The Best Ponga
09 Writer or Composer> Best Ponga
10 Genre> Blues
11 Track number> 02
12 Year> 1998
13 Length> 02:23
14
15 Sending track:
16 Codec:     ISO MPEG-1 Audio Layer 3

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.

Fusing MTP

Remember I mentioned earlier that libmtp is just a C library for programmers to make their applications talk to your MTP device? One such application is MTPfs, which is a FUSE file system that will mount an MTP device and allow you to write files onto the device in a much more familiar fashion.

If you look in your distro's repository, you will not find MTPfs. The only way to obtain it is to grab the source tarball off the website [3] and install it:

$ tar xvf mtpfs-0.9.tar.gz -C /tmp
$ cd /tmp/mtpfs-0.9
$ ./configure
$ make
$ su -c "make install"
OR
$ sudo make install

Once it is installed, create a mount point:

$ sudo mkdir /media/jogpod

Now you can plug in the device, wait for it to settle down, and the mount it with mtpfs:

$ mtpfs /media/jogpod

In the mounted directory, you'll see the folders for Images, Music, Videos, etc. as they exist on your device. With the cp command, you can copy files to the appropriate directory, and with the rm command, you can delete files. Alternatively, you could use your desktop's file manager, but Nautilus crashed on me several times when I tried to copy files.

In the mounted device, you'll also notice a Playlists directory that contains all the playlists. To make your own playlist, use your favorite text editor to create a file that lists the name of the tracks you want in it, one per line. Then save this file with a .m3u extension and copy it over to the Playlists directory. I would suggest creating the file outside the mount point and then copying it into the mounted player.

When you're done, unmount the device with fusermount -u /media/jogpod.

MTP Meets Zune

Thanks to a .NET-speaking buddy, I was able to get my hands on a first-generation Microsoft Zune player. You would assume that because Microsoft promotes MTP so actively, you'd have no trouble working with Zune on Linux, but you would be wrong.

To begin with, the Zune uses the MTPZ protocol, which is a ZUNE-specific extension to MTP. Before any copy operation, MPTZ sends an encrypted challenge to the computer, which must respond by decrypting the challenge message with a key and sending a proper reply. To add this functionality in libmtp, the developers need authentication and a signed certificate from Microsoft to authenticate, which they don't have.

So any attempts to copy files to the Zune player will throw ACCESS DENIED errors. In fact, all you can do is list the files on the Zune and delete them. And you thought that just because you paid for the device, you could use it anyway you wanted to!

Converting Videos for MTP Devices

Many media devices also play videos. But unlike music files, you can't just copy any video files onto your MTP player. To get them to play properly, the videos have to be transcoded into the proper format and resolution with the use of tools such as FFmpeg, Mencoder, or Handbrake.

To transcode videos for your device, refer to the vendor documentation and jot down the limitations you're working with in terms of video resolution, aspect ratio, video and audio bit rate, sampling rate, and number of channels, as well as the video container and codec supported by your device.

Some device vendors try to throw you off balance with their own custom format, such as Samsung and its Samsung Audio Video Interleaving (SVI) format, which is nothing but their own AVI container format. You can use the SVIcoder [7] tool to transcode videos for playback on Samsung devices.

MTP Support in Music Players

In addition to the command-line tools discussed in this article, a couple of popular music players on the Linux desktop support MTP devices: Amarok for KDE users and RhythmBox for Gnome. Both RhythmBox and Amarok include code from libmtp, so just make sure you grab the most recent version from your distro's repositories.

To get your MTP devices to work on RhythmBox, you will have to activate the MTP plugin. First go to Edit | Plugins, which will open the Configure Plugins dialog box (Figure 1). Next, scroll down the list of plugins to find and enable the Portable Player - MTP plugin. Now connect the device, and RhythmBox will pick it up.

Figure 1: RhythmBox bundles MTP support as a plugin.

Similarly, with Amarok, you need to activate support for MTP devices. To do so, head over to Settings | Configure Amarok | Media Device (Figure 2). You'll have to add your device manually with the Add Device option, which involves selecting the MTP plugin from a pull-down list and entering a name for the device. After adding the device, return to the main interface and click on the Devices tab. There, select MTP Media Device from the pull-down list and click on the Connect button.

Figure 2: If you're lucky, Amarok will autodetect your MTP device.

Once your device is listed, you can drag and drop files into the player and play music from it.

A few other applications support MTP devices. For example, if you have one of the Creative Nomad players, you can transfer music onto it with Gnomad2 [4], which also is written by one of the libmtp developers. The Banshee and Audacious music players will talk with MTP devices as well.

If you are a developer and want to write your own app for communicating with MTP devices, you can use either the original libmtp library written in C, the Ruby wrapper [5], or the Python wrapper [6].

Conclusion

Although an MTP device might not behave like a simple mass storage device at first glance, thanks to the developers of libmtp, you will hardly notice any differences when you use MTP devices on Linux.

INFO
[1] libmtp Project: http://libmtp.sourceforge.net/
[2] Download libmtp: http://libmtp.sourceforge.net/download.php
[3] MTPfs FUSE filesystem for reading MTP devices: http://www.adebenham.com/mtpfs/
[4] Gnomad2 for several players from Creative and Dell: http://gnomad2.sourceforge.net/
[5] Ruby wrapper for libmtp: http://rubyforge.org/projects/libmtp/
[6] Python wrapper for libmtp: http://launchpad.net/pylibmtp
[7] SVIcoder: http://svicoder.sourceforge.net/
THE AUTHOR

Mayank Sharma has written for various Linux publications. He has published two books through Packt on administering Elgg and Openfire. Occasionally he teaches FLOSS technologies. You can reach him via www.geekybodhi.net.