FileNotFoundError试图获取mp3文件

时间:2016-04-14 15:36:03

标签: python django pydub

我尝试使用pydub将上传的mp3转换为某个比特率,使用以下代码:

console.log("%c" + message, "color:" + JSON.stringify(color) + ";font-weight:bold; font-family:'Helvetica Neue'");

我收到此错误:

  

FileNotFoundError:[WinError 2] Systemet finner ikke angitt fil

调用from pydub import AudioSegment def process_mp3(mp3, id): print(mp3) # media/track1-original audio = AudioSegment.from_mp3(mp3) bitrates = [128, 192, 256, 320] for bitrate in bitrates: audio.export(settings.MEDIA_ROOT + '/' + 'track' + id + '-' + bitrate, format="mp3", bitrate=bitrate + 'k') def save_file(file, name): with open(default_storage.path(settings.MEDIA_ROOT + '/' + name), 'wb+') as destination: for chunk in file.chunks(): destination.write(chunk) return name 的代码是(注意:track_obj是Django模型对象,ID来自数据库):

process_mp3

显然,mutagen可以正常读取文件,但pydub不能。是什么给了什么?

2 个答案:

答案 0 :(得分:0)

我没有玩mutagen,因此无法让track_obj.id工作。它是mutagen属性吗?

此外,由于track_obj.id无效,因此track使用audio["TIT2"].text[0]通过调用mutagen.id3取代{/ 1}

我删除了对external paths的所有显式引用,并简化了用于测试目的的路径和代码。

我还注意到您尝试使用concatenatestringpath, names int bitrate等)。这需要一些修复。

此代码不使用save_file function,因此我无法对此发表评论。

无论如何,在所有这些之后,下面的代码可以运行,并且能够通过mp3提供bitrate来保存pydubfrom mutagen.mp3 import MP3 from mutagen.id3 import ID3 from pydub import AudioSegment def process_mp3(mp3, id): print(mp3) # media/track1-original audio = AudioSegment.from_mp3(mp3) bitrates = [128, 192, 256, 320] for bitrate in bitrates: export_file = my_media_path + '/' + 'track_' + id + '-' + str(bitrate) +'.mp3' print ("exporting %s" % export_file) audio.export(export_file, format="mp3", bitrate= str(bitrate) + 'k') def save_file(file, name): with open(default_storage.path(settings.MEDIA_ROOT + '/' + name), 'wb+') as destination: for chunk in file.chunks(): destination.write(chunk) mp3 = "test.mp3" mp3_info = MP3('media/' + mp3); # This works my_media_path = 'media' audio = ID3('media/' + 'test.mp3') #print "audio_info = ",audio track = audio["TIT2"].text[0] process_mp3('media/' + mp3, str(track)) 个文件。

这是工作代码,希望这有帮助

工作代码:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
media/test.mp3
exporting media/track_End Matter-128.mp3
exporting media/track_End Matter-192.mp3
exporting media/track_End Matter-256.mp3
exporting media/track_End Matter-320.mp3
>>> 

输出:

The column status returned was: "The value violated the integrity constraints for the column.". 

答案 1 :(得分:0)

这可能无法解决您的问题,但是pydub也接受文件句柄(而不是路径):

试试这个吗?

/etc/mysql/my.cnf