【发布时间】:2014-03-23 22:10:11
【问题描述】:
我一直在研究一个 Python 问题。我正在尝试使用 Echoprint API 来整理我的音乐。所以我正在编写一些代码来为我做这件事。
这就是 API 的工作原理:
- 将歌曲名称作为命令行参数。
- 给出适当的结果。
但我正在编写一个必须“在内部”执行此操作的脚本。如中所示,脚本应该获取文件并执行查找并将结果输出到终端。 (基本上 - 没有提供命令行参数)
那么无论如何将文件传递给函数? 我知道这听起来很傻,但这是我无法解决的问题。
如果我使用 os.walk() 等,它会向我的查找函数返回一个 str 对象作为参数。我希望将音频文件作为参数传递。
这是将歌曲作为命令行参数的代码:
import sys
import os
import pyechonest.config as config
import pyechonest.song as song
config.CODEGEN_BINARY_OVERRIDE = os.path.abspath("/Users/******/python/minger/echoprint-codegen-master/echoprint-codegen")
config.ECHO_NEST_API_KEY='*****'
def lookup(file):
# Note that song.identify reads just the first 30 seconds of the file
fp = song.util.codegen(file)
if len(fp) and "code" in fp[0]:
# The version parameter to song/identify indicates the use of echoprint
result = song.identify(query_obj=fp, version="4.11")
print "Got result:", result
print result[0]
if len(result):
print "Artist: %s (%s)" % (result[0].artist_name, result[0].artist_id)
print "Song: %s (%s)" % (result[0].title, result[0].id)
else:
print "No match. This track may not be in the database yet."
else:
print "Couldn't decode", file
if __name__ == "__main__":
if len(sys.argv) < 2:
print >>sys.stderr, "Usage: %s <audio file>" % sys.argv[0]
sys.exit(1)
lookup(sys.argv[1])
【问题讨论】: