Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Bash scripting with mtp-tools

  1. #11
    Join Date
    Nov 2010
    Beans
    44
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting with mtp-tools

    Quote Originally Posted by glennric View Post
    Hmm, that is odd. Yeah, if you can send me the core dump it would help. It seems to be having a problem loading album art from the device.
    I've uploaded a dump here.

    --

    In the libmtp documentation are examples of mtp-programs, some of which seem to resemble the mtp-tools. I've tried to compile these, but so far I'm stuck at this point:
    Code:
    cerapter@Ancalagon:~/Downloads/MTP/c-attempt$ gcc sendtr2.c -lmtp -o sendtr2
    sendtr2.c:12:20: fatal error: config.h: No such file or directory
    compilation terminated.
    cerapter@Ancalagon:~/Downloads/MTP/c-attempt$
    Whether this is due to a missing package, incorrect way of compiling, incomplete gcc installation, or some other reason, I do not yet know. In fact, a range of includes in this c file seem to be out of reach:
    Code:
    #include "config.h"
    #include "common.h"
    #include "util.h"
    #include "connect.h"
    #include "libmtp.h"
    #include "pathutils.h"
    The exception is libmtp.h, which seems to be located correctly by the compiler. I guess these files could be part of the source code, which I do not yet have the wits to acquire.
    Last edited by Cerapter; July 22nd, 2012 at 09:22 PM.

  2. #12
    Join Date
    Nov 2010
    Beans
    44
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting with mtp-tools

    I finally taught myself how to get at source codes. At first I thought I had to figure out git, but then I stumbled upon this:
    Code:
    sudo apt-get build-deb libmtp
    sudo apt-get source libmtp
    Once I realized where the source was put (current directory), I soon found the original C files of the mtp-tools. After some selective commenting-out and minimizing of printf() commands, I navigated to the root folder of the source package and compiled:
    Code:
    ./configure
    make
    make install
    I now have a unique build of the mtp-tools that output exactly what I want. I went ahead and tried out my own scripts (posted here in case anyone else want to try and do something like this):

    /home/cerapter/scripts/playlist_main.sh:
    Code:
    #!/bin/bash
    
    ind_music=87
    ind_max=500
    
    pFile=$(zenity --file-selection --filename="/home/cerapter/Downloads/"  --title="Choose your CSV playlist")
    
    if [ $? != 1 ]; then
        pName=$(zenity --entry --title="Playlist name" --text="Enter the name of the playlist (0 for guess)" --entry-text "0")
        # apparently doesn't work
        if [ $? -eq 1 ]; then 
            pName="Blubb gakk"
        fi
        
        today=$(date +%m.%d)
        gFolder="${today} ${pName}"
        
        echo "Executing: mtp-newfolder \"${gFolder}\" ${ind_music} 0"
        
        # don't need folder ID, but get it anyway
        ind_folder=$(mtp-newfolder "${gFolder}" ${ind_music} 0)
        wait ${!}
            
        echo "Created folder with ID ${ind_folder}"
    
        arg_accum=""
        for (( c=0; c<=ind_max; c++ ))
        do
            pArgs=$(python /home/cerapter/scripts/playlist_transfer.py "${pFile}" "${pName}" "${gFolder}" $c)
            command="mtp-sendtr ${pArgs}"
            
            if [ "$pArgs" == "STOP" ]; then
                echo "Reached end of CSV list."
                break
            fi
            
            echo "Executing: 'mtp-sendtr ${pArgs}'"
            
            # get track IDs!
            ind_track=$(eval $command)
            wait ${!}
            
            echo "Sent track to track ID ${ind_track}"
            
            arg_accum="${arg_accum} -i ${ind_track}"
        done
        
        echo "Executing: mtp-newplaylist -n \"${pName}\" ${arg_accum}"
        
        # and then get playlist ID (don't need this either)
        ind_list=$(mtp-newplaylist -n "${pName}" $arg_accum)
        wait ${!}
            
        echo "Made playlist with ID ${ind_list}"
    fi
    /home/cerapter/scripts/playlist_transfer.py:
    Code:
    #!/usr/bin/python
    
    import string, csv, sys
    
    # from bash script:
    playlist_file = str(sys.argv[1])
    playlist_name = str(sys.argv[2])
    gFolder = str(sys.argv[3])
    counter = int(sys.argv[4])
    
    # get music player directory
    path = "/Music/" + gFolder + "/"
    
    # get the current line of the CSV
    fList = csv.DictReader(open(playlist_file, 'r'))
    fList = list(fList)
    
    # if last line is reached, say STOP!
    try:
        row = fList[counter]
        # use the data from this line
        fPath = row['Folder'] + "/" + row['Filename']
        gPath = path + row['Filename']
    
        # build the mtp-sendtr command
        lTrack = '-q -t "'+row['Title']+'"'
        lTrack = lTrack + ' -a "'+row['Artist']+'"'
        lTrack = lTrack + ' -l "'+row['Album']+'"'
        lTrack = lTrack + ' -g "'+row['Genres']+'"'
        lTrack = lTrack + ' -n '+row['Track']
        lTrack = lTrack + ' -y '+row['Year']
        lTrack = lTrack + ' -d '+row['Length']
        lTrack = lTrack + ' -s 0 "'+fPath+'" "'+gPath+'"'
        #oTrack.wavecodec = 
    
        print lTrack
    except IndexError:
        print 'STOP'
    I have not yet succeeded. I get this error output after calling a command that should work, and which works if I run it by itself manually afterwards:
    Code:
    Executing: mtp-sendtr -q -t "Chronologie Part.4" -a "Jean Michel Jarre" -l "Chronologie" -g "New Age" -n 4 -y 1993 -d 239 -s 0 "/home/cerapter/Musikk/Artister/Jean Michel Jarre - Chronologie Part.4.mp3" "/Music/07.23 Snupp tupp/Jean Michel Jarre - Chronologie Part.4.mp3"
    Device 0 (VID=041e and PID=4162) is a Creative ZEN X-Fi.
    usage: sendtr [ -D debuglvl ] [ -q ]
    -t <title> -a <artist> -A <Album artist> -w <writer or composer>
        -l <album> -c <codec> -g <genre> -n <track number> -y <year>
           -d <duration in seconds> -s <storage_id> <local path> <remote path>
    (-q means the program will not ask for missing information.)
    I'm now going to look into the C files again to trace back and troubleshoot this error. It does take a while, since between each attempt I have to wake up, unmount and clean up the player..

    EDIT: Script works now, updated code.
    Last edited by Cerapter; July 24th, 2012 at 09:26 AM.

  3. #13
    Join Date
    Jul 2007
    Location
    Burlington, NC
    Beans
    1,995
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash scripting with mtp-tools

    Forgive me for asking, but have you tried the MTP plugin for Rhythmbox?

    Or even better, just mounting it with mtpfs.
    Last edited by asmoore82; July 23rd, 2012 at 08:34 PM.
    Give me Free Software or Give me Death!

  4. #14
    Join Date
    Nov 2010
    Beans
    44
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting with mtp-tools

    Quote Originally Posted by asmoore82 View Post
    Forgive me for asking, but have you tried the MTP plugin for Rhythmbox?

    Or even better, just mounting it with mtpfs.
    Of course, I might've overlooked it. But I'm pretty sure I tried that and that Rhythmbox crashed. Amarok and Clementine could find the player, but not do anything with it. I've also tried Qlix and MTP Navigator (both crash with strange error messages, or simply segmentation fault). gMTP I think also crashed, although it wouldn't be able to handle playlists anyway.

    In fact, the player mounts fine and I can access it in nautilus. I can even transfer files by drag and drop and edit playlists in gedit - but these changes are unnoticed by the player. I realized that nautilus does not tell the player that a music file is a music file, so it's overlooked. And as for playlists, when I edit them, the name vanishes in the player, and if I add tracks, they are ignored.

    I'm suspecting this is due to quirks of how MTP is implemented in my player, and it might be hard to work around it. Luckily, the mtp-tools work perfectly, thus my approach so far.
    Last edited by Cerapter; July 23rd, 2012 at 08:52 PM.

  5. #15
    Join Date
    Nov 2010
    Beans
    44
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash scripting with mtp-tools

    My script now works! It will load a .csv file (which is how gmusicbrowser exports playlists), make a new folder, send all tracks of the playlist to that folder, and add them all to a brand new playlist!

    The only problem is that currently, it takes more than 10 seconds for each and every file. This due to the fact that it connects and disconnects the mp3 player between every upload. I guess I'd have to do some hard core rewriting of the mtp-tools to make it otherwise.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •