2014-10-03 62 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