2015-09-11 38 views
0

我運行FFmpeg的命令的音頻文件轉換成MP3在我的Rails應用程序:如何知道在Ruby中ffmpeg轉換是否成功?

# Convert to MP3 
conversion_result = `ffmpeg -i "#{Refile::cache.directory}/#{self.file.id}" -f mp3 "#{_path_to_mp3}" 2>&1` 

轉換後,conversion_result一個示例如下:

「的ffmpeg版本N-74748- gbaeb8f5版權所有(c)2000-2015 FFmpeg 開發者\ n內置gcc 4.8(Ubuntu 4.8.4-2ubuntu1〜14.04)\ n 配置:--extra-libs = -ldl --prefix =/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-am rnb --enable-libopencore-amrwb --disable-decoder = amrnb --disable-decoder = amrwb --enable-libpulse --enable-libdcadec --enable-libfreetype --enable-libx264 --enable-libx265 --enable -libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid - -enable-libvo-aacenc -enable-libvidstab \ n libavutil 54. 31.100/54. 31.100 \ n libavcodec 56. 59.100/56. 59.100 \ n libavformat 56. 40.101/56. 40.101 \ n libavdevice 56. 4.100/56。 4.100 \ n libavfilter 5. 40.100/5.40.100 \ n libavresample 2. 1.0/2. 1. 0 \ n libswscale 3.101/ 3. 1.101 \ n libswresample 1. 2.101/1. 2.101 \ n libpostproc 53 3.100/53. 3.100 \ nInput#0,mov,mp4,m4a,3gp,3g2,mj2,來自'../mey_attachments/cache/25888dc028deaabaee4d4de19d4726573860dbce0b907131e9919d294113':\n 元數據:\ n major_brand:3gp4 \ n minor_version:0 \ n compat ible_brands:isom3gp4 \ n creation_time:2015-09-01 16:34:01 \ n 持續時間:00:00:08.57,開始:0.000000,比特率:426 kb/s \ n流

0:0(eng ):Audio:aac(LC)(mp4a/0x6134706D),44100 Hz,mono,fltp,48 kb/s(默認)\ n元數據:\ n creation_time:2015-09-01 16:34:01 \ n

handler_name:SoundHandle \ nOutput#0,MP3,以 「/tmp/Voice00045.mp3':\n元數據:\ n major_brand:3gp4 \ n minor_version:0 \ n compatible_brands:isom3gp4 \ n TSSE: Lavf56。 40.101 \ n Stream#0:0(eng):Audio:mp3(libmp3lame),44100 Hz, mono,fltp(默認)\ n元數據:\ n creation_time:2015-09-01 16:34 :01 \ n handler_name:SoundHandle \ n encoder:Lavc56.59.100 libmp3lame \ nStream映射:\ n Stream#0:0 - >#0:0(aac(native) - > mp3 (libmp3lame))\ n按[q ]停止,[?]尋求幫助\ nsize = 67kB time = 00:00:08.56 bitrate = 64.5kbits/s \ nvideo:0kB audio:67kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.465427%\ n「

如何在命令執行後確保轉換成功?

回答

2

你需要命令的全文輸出嗎?

因爲如果不是,我會建議你使用system command that will return true如果命令終止與退出代碼0(成功)和假或否則如果它失敗。

conversion_success = system %Q[ffmpeg -i "#{Refile::cache.directory}/#{self.file.id}" -f mp3 "#{_path_to_mp3}" 2>&1] 

注意,在這個例子中我用%Q[...]的字符串,以避免逃避" S和尚未能有串插了命令。

+0

現在;我使用系統命令並檢查結果; '如果$ ?.成功? && File.exists?(_ path_to_mp3)'。這是正確的方式嗎? –

+0

好的,我沒有想到那個。你可以簡單地堅持原來的解決方案並使用'$ ?. success',或者你使用system,然後你有兩個選項(檢查系統的返回值或者使用'$ ?. success?'),我想它沒有'很重要,而且更像是一種風格。我認爲沒有正確和錯誤的做法。 – evotopid

+0

你能舉一個例子來說明如何使用'system'來檢查結果嗎? –

相關問題