Getting your MTP devices working on Linux

Music Library

Author(s):

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

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.

Read full article as PDF:

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

News