source: subversion/applications/utils/amr2wav/amr2wav.py @ 30601

Last change on this file since 30601 was 14984, checked in by gjones, 10 years ago

Added separate gpsmid and gpsmid_lc modes because some phones use all
lower case filenames (requiring gpsmid_lc) but others use part capitalised
filenames. Mode gpsmid uses the filename used in the gpx file.
This was necssary when I changed from a K510i to a W880i phone.

File size: 7.0 KB
Line 
1#!/usr/bin/python
2############################################################################
3#   gpxamr2wav: Convert amr audio references in a GPX file to wav format.
4#    Copyright (C) 2009   Graham Jones
5#
6#    This program is free software: you can redistribute it and/or modify
7#    it under the terms of the GNU General Public License as published by
8#    the Free Software Foundation, either version 3 of the License, or
9#    (at your option) any later version.
10#
11#    This program is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#    GNU General Public License for more details.
15#
16#    You should have received a copy of the GNU General Public License
17#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19###########################################################################
20#
21# NAME: gpxamr2wav.py [-i <gpxinputfile>] [-o <gpxoutputfile>] [-m mode]
22# DESC: Adds <link href="filename.wav"> elements to the input gpx file
23#       to produce the specified output gpx file.
24#       The method of determining the filename depends on the mode.  Valid modes
25#       are:
26#           gjGPSMid - Graham's version of GPSMid which adds link elements.
27#           name     - uses the element's name to determine the filename.
28#       The audio files found in the input GPX file is converted to wav format
29#       using ffmpeg.
30#       It writes the gpx file to the output, with the only change
31#       being the <link> elements point to the new wav files instead of
32#       the amr ones.
33# HIST: 10apr2009  GJ  ORIGIAL VERSION (0.1).
34#       13apr2009  GJ  Added different modes as options (0.2)
35#
36#   Copyright 2009  Graham Jones (grahamjones139 <at> gmail.com).
37#
38###########################################################################
39
40import sys
41import os
42from parseGPX import parseGPX
43from optparse import OptionParser
44
45usage = "Usage %prog [options]\n\
46Reads the input GPX file and adds <link> tags to point to audio files associated\
47with the data.  \n\
48The method of determining the audio filename is specified by the mode option.\n\
49the difference between mode 'gpsmid' and 'gpsmid_lc' is that 'gpsmid_lc' converts\n\
50the filenames to lower case whereas 'gpsmid' uses the case from the gpx file.\n\
51It is assumed that the audio files are in amr format, and so they\
52are converted to .wav format using ffmpeg."
53version = "0.3"
54
55modes = ["gjgpsmid","gpsmid_lc","gpsmid"]
56
57parser = OptionParser(usage=usage,version=version)
58parser.add_option("-i", "--infile", dest="infilename", 
59                  help="input file name (default: waypoints.gpx)",
60                  metavar="INFILE")
61parser.add_option("-o", "--outfile", dest="outfilename",
62                  help="filename to use for output (default: waypoints_wav.gpx)",
63                  metavar="OUTFILE")
64parser.add_option("-m", "--mode", dest="modestr",
65                  help="method of identifying files to convert (default: gpsmid)\
66 valid values are %s" % modes,
67                  metavar="MODE")
68parser.add_option("-v", "--verbose", action="store_true",dest="verbose",
69                  help="Include verbose output")
70parser.add_option("-d", "--debug", action="store_true",dest="debug",
71                  help="Include debug output")
72parser.set_defaults(
73    infilename="Waypoints.gpx",
74    outfilename="waypoints_wav.gpx",
75    modestr = "gpsmid",
76    debug=False,
77    verbose=False)
78(options,args)=parser.parse_args()
79
80if (options.debug):
81    options.verbose = True
82    print "options   = %s" % options
83    print "arguments = %s" % args
84       
85#if len(args)<1:
86#    print "Error: You must supply at least one GPX file to analyse.\n"
87#    sys.exit(-1)
88#else:
89print "Reading Input File %s." % options.infilename
90pgpx = parseGPX(options.infilename,options.debug,options.verbose)
91
92try:
93    modeno = modes.index(options.modestr)
94    if (options.debug): print "Using mode number %d (%s) to detect filenames" \
95            % (modeno,options.modestr)
96except ValueError:
97    modeno = -1
98    print "Error: Specified mode %s does not exist.  Valid modes are: %s" \
99        % (options.modestr,modes)
100    exit(-1)
101
102
103print "Opening Output File %s for output." % options.outfilename
104of = open(options.outfilename,'w')
105of.write("<?xml version='1.0' encoding='UTF-8'?>")
106of.write("<gpx version='1.1' creator='amr2wav' xmlns='http://www.topografix.com/GPX/1/1'>")
107print "Processing all waypoints in input file...."
108for waypt in pgpx.wayPts:
109    fname = "undefined"
110    if options.debug: print waypt
111    of.write("<wpt lat='%s' lon='%s'>\n" % (waypt['lat'],waypt['lon']))
112    if 'name' in waypt:
113        of.write("<name>%s</name>\n" % waypt['name'])
114        if modeno==1 or modeno==2:   # gpsmid mode
115            if options.debug: print "modeno=%d" % modeno
116            namestr = waypt['name']
117            nameparts = namestr.split('AudioMarker-')
118            if options.debug: print "nameparts=%s" % nameparts
119            fnamebase = nameparts[1]
120            if modeno==1:
121                fname = fnamebase.lower() + '.amr'
122            else:
123                fname = fnamebase + '.amr'
124            if options.debug: print "fname = %s" % fname
125    if 't' in waypt:
126        of.write("<com><time>%s</time></com>\n" % waypt['time'])
127    if 'link' in waypt:
128        if modeno==0:   # gjGPSMid Mode
129            if options.debug: print "link=%s" % waypt['link']
130            linkstr = waypt['link']
131            linkparts = linkstr.split('"')
132            fileURL = linkparts[1]
133            fileURLparts = fileURL.split('//')
134            fname = fileURLparts[1]
135        #print "fname = %s" % fname
136        else:
137            if (options.debug): print "Ignoring existing link tag in waypoint %s, because you specified mode %s" % (waypt,options.modestr)
138    # Do the actual conversion from amr to wav, if we have identified a filename
139    # to convert.
140    if fname != "undefined":       
141        fnameparts = fname.split('.')
142        fnamebase = fnameparts[0]
143        fnameext = fnameparts[1]
144        if os.path.isfile("%s.%s" % (fnamebase,fnameext)):
145            print "Converting file %s.%s" % (fnamebase, fnameext)
146            if options.debug:
147                os.system("ffmpeg -y -v 0 -i %s.%s %s.%s" % \
148                              (fnamebase,fnameext,fnamebase,"wav"))
149            else:
150                os.system("ffmpeg -y -v 1 -i %s.%s %s.%s" % \
151                          (fnamebase,fnameext,fnamebase,"wav"))
152                # This was supposed to just write ffmpeg output to a log file, but it doesn't work.
153#                os.system("ffmpeg -y -v 1 -i %s.%s %s.%s 2>! amr2wav.log" % \
154#                         (fnamebase,fnameext,fnamebase,"wav"))
155
156            of.write('<link href="file://%s/%s.%s">audio</link>\n' \
157                         % (os.getcwd(),fnamebase,"wav"))
158        else:
159            print "Warning, File %s.%s does not exist - skipping conversion.." % \
160                (fnamebase,fnameext)
161    else:
162        print "\nOh no, have not managed to extract a filename from waypoint %s - skipping\n" % waypt
163    of.write("</wpt>\n")
164of.write("</gpx>\n")
165of.close()
166
167
Note: See TracBrowser for help on using the repository browser.