A Sync Script for MTP Device

I’m less concerned about managing albums on my Samsung S3 from within Linux than I am “changing” content like podcasts, images, shopping and to-do lists, etc. This is no problem at all in Windows because MTP is perfectly supported under it.

The S3 has a file structure which separates “datacasts” from other audio content. I’d been using a quick little script that would copy every ogg, mp3, and/or wma file in the current directory (specifically, ~/podcasts) to my S3’s Datacasts directory. It was fine for doing that, but I wanted it to do a little more than that so I could use one directory as a “sync” folder for more file types. Then I could move all content I want to that folder, run one script, and my podcasts would go to Datacasts, text files would go to Texts, and pics would go to Pictures; as I’ve written before, the device isn’t fine-tuned enough to automatically put each kind of file in an appropriate directory so I wanted my script to assure everything goes exactly where I want it. Otherwise I’d have to go to the “File manager” directory and trace through it to other folders to find my content. This way I know exactly where everything is.

One important thing to note about libmtp and mtp-tools (mtp-examples in Fedora) is that directories are referred by their ID numbers rather than their names. It’s easy to get the directory ID numbers for these using the mtp-folders command. For example, I used the following to get the “Pictures” directory:

% mtp-folders | grep -i pictures

This reported back the folder “Pictures” with an ID number 32776. Other MTP devices will likely have different ID numbers for different things so double check your own device before using the following script.

Once I had the relevant directory numbers, I could set up a few functions to send files to them through a case command for each file type I want to manage. Here’s the current version of my move_podcasts.sh script:

# lucky13linux.wordpress.com - Wed Jul 29 14:33:14 CDT 2009

# moves files from PWD to MTP device via mtp-sendfile
# and deletes the file from PWD. Use this for podcasts,
# text files, and images rather than for music kept
# on the system. 

# mtp-folders for my Samsung S3
# 32773 = Datacasts
# 32774 = Texts
# 32776 = Pictures

function move_podcast_files  {
	mtp-sendfile $SYNC_THIS_FILE 32773
	echo "removing $SYNC_THIS_FILE from $PWD..."

function move_text_files  {
	mtp-sendfile $SYNC_THIS_FILE 32774
	echo "removing $SYNC_THIS_FILE from $PWD..."

function move_image_files  {
	mtp-sendfile $SYNC_THIS_FILE 32776
	echo "removing $SYNC_THIS_FILE from $PWD..."

	case $SYNC_THIS_FILE in
		*.mp3)	move_podcast_files                                 ;;
		*.ogg)	move_podcast_files                                 ;;
		*.wma)	move_podcast_files                                 ;;
		*.txt)	move_text_files                                    ;;
		*.TXT)	move_text_files                                    ;;
		*.jpg)	move_image_files                                   ;;
		*.jpeg)	move_image_files		                   ;;
		*.JPG)	move_image_files		                   ;;
		*) echo "are you sure $SYNC_THIS_FILE goes to MTP device?" ;;

Nothing fancy, and someone will no doubt have a better solution (feel free to opine) or suggest moving some of the repetitive commands in each function to the for loop. It’s a work in progress and works the way I want. Caveats and additional points:

  • I use mksh and edited this to /bin/sh; change to suit your needs if /bin/sh doesn’t link to your preferred shell. EDIT (sorry!): That includes possibly having to edit the functions if your shell (e. g., CSH or TCSH?) uses C/POSIX functionname () {…} semantics rather than function functionname {…}.
  • Add or remove file types based on your own needs. I convert all my images to jpg for convenience.
  • I used single entries for each file type because I may end up adding more to the functions, e. g., adding custom id tags to mp3 and/or oggs (can’t add tags to wma in Linux as far as I know).
  • I haven’t bothered putting videos on my S3 even though it’s capable. I don’t even know if/how I can convert to the format Samsung requires videos be in to play on their devices. RTFM.
  • Delete the rm/echo commands in the functions if you want to keep local copies.
  • In my cheap original script for ogg, mp3, wma, I used ‘for FILETYPE in *.{ogg,wma,mp3}’ which resulted in error messages if one or more weren’t in the directory.
  • I’ll most likely add POD_SYNC_DIR so I can run the script from any directory or from a dynamic directory (dmenu or ion3’s F3 menu) which reads from $PATH. No big deal because I tend to wget files directly to ~/podcasts and always have a terminal open. Whatever.

Finally, mtp-tools (mtp-examples in Fedora) has the mtp-delfile command which is a bit clumsy. To remove files, I tend to delete them directly from the S3 after I listen or use gphotofs (fuse) to mount the device and delete or copy back files I want to keep. In fact, I highly recommend using gphotofs or other applications utilizing libgphoto2 for copying files from a MTP device back to a computer. Unfortunately, libgphoto2 doesn’t copy/move files and directories from computer to device or else it would be ideal, but it’s for cameras rather than media devices. Maybe the libgphoto people and libmtp people can do more collaboration and maybe even merge their projects for better all-around support of cameras and media players using PTP/MTP.

UPDATE 2009-07-29 20:01 CDT: Version 2 of the above script now does rudimentary logging which, when I’m finished, will maintain a list of the file ID numbers to (hopefully) ease removal of files without using the other utilities mentioned in the last paragraph.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: