6

I am running a OSX, don't know tons about video conversions. But I have like 200 videos that are all in mp4 format and won't play in Firefox. I need to convert them to ogg to use the html5 video tag.

These files live in a folder structure that makes it hard to do files one at a time. I would like the bash command or Ruby command to go through all child folders and find all .mp4's and convert them.

I found one reference of how to do this with Google: http://athmasagar.wordpress.com/2011/05/12/a-bash-script-to-convert-mp4-files-to-oggogv/

#!/bin/bash
for f in $(ls *mp4 | sed ‘s/\(.*\)\..*/\1/’)
do
ffmpeg -i $f.mp4 -acodec vorbis -vcodec libtheora $f.ogg
done

But have no idea how to convert this from linux to osx.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
TJ Sherrill
  • 2,465
  • 7
  • 50
  • 88
  • Option '-sameq' does NOT mean 'same quality' [https://ffmpeg.org/trac/ffmpeg/wiki/Option%20'-sameq'%20does%20NOT%20mean%20'same%20quality'](https://ffmpeg.org/trac/ffmpeg/wiki/Option%20%27-sameq%27%20does%20NOT%20mean%20%27same%20quality%27) –  Apr 03 '13 at 19:59
  • This answer isn't pertinent to the question since it makes no reference to using that option. – Marcelo De Polli Apr 03 '13 at 20:18

2 Answers2

8

While the direct answer to your question would be like this:

#!/bin/bash
MOVIES=~/Movies/
find "$MOVIES" -name '*.mp4' -exec sh -c 'ffmpeg -i "$0" -sameq "${0%%.mp4}.ogg"' {} \;
exit;

I think you might find better results using the VP8 or webm codec as it will give you much better results and is actually preferred in modern versions of Firefox. Given that, you should try this:

#!/bin/bash
MOVIES=~/Movies/
find "$MOVIES" -name '*.mp4' -exec sh -c 'ffmpeg -i "$0" -sameq "${0%%.mp4}.webm"' {} \;
exit;

Both of these methods WILL result in a loss of quality in your resulting videos as they are re-encoding the already encoded material and, in my opinion, even the webm codec is not nearly as good as a properly encoded MP4 using the h.264 codec.

Jim Martin
  • 96
  • 1
  • Any way to use ffmpg to set the frame rate or other settings to improve quality in the command? – TJ Sherrill May 28 '12 at 04:56
  • Sure the -r tag will set the frame rate and you can set the bit rate with the -b:v tag so you could use something like this: `#!/bin/bash MOVIES=~/Movies/ find "$MOVIES" -name '*.mp4' -exec sh -c 'ffmpeg -i "$0" -r 23.976 -b:v 800k -bt 800k -sameq -sameq "${0%%.mp4}.webm"' {} \; exit;` – Jim Martin May 28 '12 at 05:15
  • Do not suggest usage of `-sameq` in this case. This option does not mean "same quality", as the docs used to imply, and probably does not do what you think it does. It is not designed to be used between formats that do not share the same quantizer scale. – llogan May 28 '12 at 23:32
  • 2
    Note that `-sameq` might need to be replaced by something like `-qscale 0` or equivalent (as suggested by ffmpeg CLI output in the latest version (git-2012-11-23-e9c3723) – foobar Nov 23 '12 at 11:45
  • The example in this article worked wihtout any changes: https://thethemefoundry.com/blog/convert-mp4-to-webm/ – Nikolay Tsenkov Aug 07 '14 at 09:37
6

This is using Ruby, assuming the ffmpeg you used is correct:

Dir.glob("**/*.mp4").each do |filename|
  new_filename = File.join(
    File.dirname(filename),
    "#{File.basename(filename, ".mp4")}.ogg")
  `ffmpeg -i "#{filename}" -acodec vorbis -vcodec libtheora "#{new_filename}"`
end

Dir.glob with "**/*.mp4" recursively matches all the files within subdirectories witha .mp4 extension.

pigoz
  • 302
  • 2
  • 6
  • Thanks for the answer but it returns: ruby convert.rb convert.rb:3:in `block in
    ': undefined local variable or method `file' for main:Object (NameError) any ideas
    – TJ Sherrill May 27 '12 at 20:12