2014-10-03 64 views
1

我正在编写一个脚本,需要下载和播放位于单独的.xls文件中的音频文件(基于词汇表单词)。我想使用Dictionary.com下载正确的发音音频,但无法用音频方式获取音频。如何从Dictionary.com下载MP3文件在Matlab中?

例如,对于单词的URL “愚昧”,是

http://static.sfdict.com/staticrep/dictaudio/F00/F0058700.mp3

但对于单词 “好玩” 是

http://static.sfdict.com/staticrep/dictaudio/F04/F0416100.mp3

我如何可以查询Dictionary.com和下载相关的音频?有什么建议么?

回答

2

使用Fbla/Fblabla构建查询有点困难,但可以从dictionary.com获取所需的链接。像这样的东西似乎工作:

word = 'supercalifragilisticexpialidocious'; % the word that is to be found 

% build the query for dictionary.com by adding the word and read the url 
r = urlread(sprintf('http://dictionary.reference.com/browse/%s',word)); 

% examine the resulting string that contains the html 
try 
    % find the index of a certain matching sentence that indicates the beginning and end of the query 
    matchIdx = strfind(r,'audio/ogg"> <source src="http://static.sfdict.com/staticrep/dictaudio/'); 
    endIdx = strfind(r, '.mp3"></a>'); 

    % some tuning with indexes results in the exact url that needs to be called 
    % hopefully, the matchIdx and endIdx are found only once - if more, there is an error 
    mp3url = r(matchIdx+141:endIdx+3); 

    % if there is no match, the url is empty, which is also undesirable 
    assert(~isempty(mp3url)); 

catch e 
    % catch the above errors (the mp3url cannot be constructed or is empty), and maybe others 
    % (if this becomes a function, some nice pointers of what went wrong should be added) 
    error('Could not find url.'); 
end 

% query for the mp3 and store the result 
file = 'temp.mp3'; 
urlwrite(mp3url,file); 

% read the just stored file and play! 
[x,Fs] = audioread(file); 
soundsc(x,Fs); 
+0

这是非常有用的 - 我试图弄清代码 - 你可以添加一些评论,指导我通过它? (仍然不流利matlab)。如何在哪里获得音频文件的链接? – Cerberus 2014-10-03 15:42:35

+0

好的 - 完成了。如需更多帮助,请阅读[urlread]上的matlab手册(http://www.mathworks.nl/help/matlab/ref/urlread.html),[audioread](http://www.mathworks.nl/help/) matlab/ref/audioread.html),[soundsc](http://www.mathworks.nl/help/matlab/ref/soundsc.html)等。 – MeMyselfAndI 2014-10-03 15:56:10

+0

再次感谢!我正在使用MATLAB R2010a,并没有“audioread”功能,我应该如何播放MP3文件,有什么建议吗? – Cerberus 2014-10-04 15:23:26