0

I run the ffmpeg command to convert audio files to mp3 in my rails application:

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

After the conversion, an example of conversion_result is as follows:

"ffmpeg version N-74748-gbaeb8f5 Copyright (c) 2000-2015 the FFmpeg developers\n built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)\n configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --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. 1.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, from '../mey_attachments/cache/25888dc028deaabaee4d4de19d4726573860dbce0b907131e9919d294113':\n Metadata:\n major_brand : 3gp4\n minor_version : 0\n compatible_brands: isom3gp4\n creation_time : 2015-09-01 16:34:01\n Duration: 00:00:08.57, start: 0.000000, bitrate: 426 kb/s\n Stream

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

handler_name : SoundHandle\nOutput #0, mp3, to '/tmp/Voice00045.mp3':\n Metadata:\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 (default)\n Metadata:\n creation_time : 2015-09-01 16:34:01\n handler_name : SoundHandle\n encoder : Lavc56.59.100 libmp3lame\nStream mapping:\n Stream #0:0 -> #0:0 (aac (native) -> mp3 (libmp3lame))\nPress [q] to stop, [?] for help\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"

How can i make sure the conversion succeeded after the command executes?

Community
  • 1
  • 1
tozlu
  • 4,667
  • 3
  • 30
  • 44

1 Answers1

2

Do you need the full text output of the command?

Because if not I would suggest you to use the system command that will return true if the command terminates with exit code 0 (success) and false or nil if it fails.

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

Note that in the example I used %Q[...] for the string to avoid having to escape the "s and yet be able to have string interpolation for the command.

Community
  • 1
  • 1
evotopid
  • 5,288
  • 2
  • 26
  • 41
  • Right now; i use system command and check the result with; `if $?.success? && File.exists?(_path_to_mp3)`. Is it the right way? – tozlu Sep 11 '15 at 16:46
  • Ok I didn't think about that one. You could simply stick to your original solution and use `$?.success?` or you use system and then you have both options (check the return value of system or use `$?.success?`), I guess it doesn't matter and is really more a thing of style. I think there is not a right and wrong way to do it. – evotopid Sep 11 '15 at 16:51
  • Could you show an example on how to use `system` to check the result? – tozlu Sep 11 '15 at 17:14
  • 1
    In the example I provided `conversion_success` is either `true`, `false` or `nil`. Only `true` signifies success. So you could just use `if conversion_success` or `if system(...)`altough I'd prefer the first one because it's a bit cleaner... – evotopid Sep 12 '15 at 17:57