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

Last change on this file since 14562 was 14562, checked in by gjones, 11 years ago

Fixed problem with ffmpeg not working unless you specify debug mode (-d
option).

File size: 6.7 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\
49It is assumed that the audio files are in amr format, and so they\
50are converted to .wav format using ffmpeg."
51version = "0.2"
52
53modes = ["gjgpsmid","gpsmid"]
54
55parser = OptionParser(usage=usage,version=version)
56parser.add_option("-i", "--infile", dest="infilename", 
57                  help="input file name (default: waypoints.gpx)",
58                  metavar="INFILE")
59parser.add_option("-o", "--outfile", dest="outfilename",
60                  help="filename to use for output (default: waypoints_wav.gpx)",
61                  metavar="OUTFILE")
62parser.add_option("-m", "--mode", dest="modestr",
63                  help="method of identifying files to convert (default: gpsmid)\
64 valid values are %s" % modes,
65                  metavar="MODE")
66parser.add_option("-v", "--verbose", action="store_true",dest="verbose",
67                  help="Include verbose output")
68parser.add_option("-d", "--debug", action="store_true",dest="debug",
69                  help="Include debug output")
70parser.set_defaults(
71    infilename="waypoints.gpx",
72    outfilename="waypoints_wav.gpx",
73    modestr = "gpsmid",
74    debug=False,
75    verbose=False)
76(options,args)=parser.parse_args()
77
78if (options.debug):
79    options.verbose = True
80    print "options   = %s" % options
81    print "arguments = %s" % args
82       
83#if len(args)<1:
84#    print "Error: You must supply at least one GPX file to analyse.\n"
85#    sys.exit(-1)
86#else:
87print "Reading Input File %s." % options.infilename
88pgpx = parseGPX(options.infilename,options.debug,options.verbose)
89
90try:
91    modeno = modes.index(options.modestr)
92    if (options.debug): print "Using mode number %d (%s) to detect filenames" \
93            % (modeno,options.modestr)
94except ValueError:
95    modeno = -1
96    print "Error: Specified mode %s does not exist.  Valid modes are: %s" \
97        % (options.modestr,modes)
98    exit(-1)
99
100
101print "Opening Output File %s for output." % options.outfilename
102of = open(options.outfilename,'w')
103of.write("<?xml version='1.0' encoding='UTF-8'?>")
104of.write("<gpx version='1.1' creator='amr2wav' xmlns='http://www.topografix.com/GPX/1/1'>")
105print "Processing all waypoints in input file...."
106for waypt in pgpx.wayPts:
107    fname = "undefined"
108    if options.debug: print waypt
109    of.write("<wpt lat='%s' lon='%s'>\n" % (waypt['lat'],waypt['lon']))
110    if 'name' in waypt:
111        of.write("<name>%s</name>\n" % waypt['name'])
112        if modeno==1:   # gpsmid mode
113            namestr = waypt['name']
114            nameparts = namestr.split('AudioMarker-')
115            if options.debug: print "nameparts=%s" % nameparts
116            fnamebase = nameparts[1]
117            fname = fnamebase.lower() + '.amr'
118            if options.debug: print "fname = %s" % fname
119    if 't' in waypt:
120        of.write("<com><time>%s</time></com>\n" % waypt['time'])
121    if 'link' in waypt:
122        if modeno==0:   # gjGPSMid Mode
123            if options.debug: print "link=%s" % waypt['link']
124            linkstr = waypt['link']
125            linkparts = linkstr.split('"')
126            fileURL = linkparts[1]
127            fileURLparts = fileURL.split('//')
128            fname = fileURLparts[1]
129        #print "fname = %s" % fname
130        else:
131            if (options.debug): print "Ignoring existing link tag in waypoint %s, because you specified mode %s" % (waypt,options.modestr)
132    # Do the actual conversion from amr to wav, if we have identified a filename
133    # to convert.
134    if fname != "undefined":       
135        fnameparts = fname.split('.')
136        fnamebase = fnameparts[0]
137        fnameext = fnameparts[1]
138        if os.path.isfile("%s.%s" % (fnamebase,fnameext)):
139            print "Converting file %s.%s" % (fnamebase, fnameext)
140            if options.debug:
141                os.system("ffmpeg -y -v 0 -i %s.%s %s.%s" % \
142                              (fnamebase,fnameext,fnamebase,"wav"))
143            else:
144                os.system("ffmpeg -y -v 1 -i %s.%s %s.%s" % \
145                          (fnamebase,fnameext,fnamebase,"wav"))
146                # This was supposed to just write ffmpeg output to a log file, but it doesn't work.
147#                os.system("ffmpeg -y -v 1 -i %s.%s %s.%s 2>! amr2wav.log" % \
148#                         (fnamebase,fnameext,fnamebase,"wav"))
149
150            of.write('<link href="file://%s/%s.%s">audio</link>\n' \
151                         % (os.getcwd(),fnamebase,"wav"))
152        else:
153            print "Warning, File %s.%s does not exist - skipping conversion.." % \
154                (fnamebase,fnameext)
155    else:
156        print "\nOh no, have not managed to extract a filename from waypoint %s - skipping\n" % waypt
157    of.write("</wpt>\n")
158of.write("</gpx>\n")
159of.close()
160
161
Note: See TracBrowser for help on using the repository browser.