如何在Matlab中从Dictionary.com下载MP3文件?

时间:2014-10-03 14:35:15

标签: matlab audio web dictionary mp3

我正在编写一个脚本,需要下载和播放位于单独.xls文件中的音频文件(基于词汇单词)。我想使用Dictionary.com下载音频以获得正确的发音,但无法找到获得音频的合成方式。

例如,“fatuity”一词的网址是

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

但是“有趣”这个词是

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

如何查询Dictionary.com并下载相关音频?有什么建议?

1 个答案:

答案 0 :(得分: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);