The ffmpeg command you need is something like:
ffmpeg -i input.foo -c:v libx264 -profile:v high -level 5 -preset slow -b:v $videobitrate -an -pass 1 output.bar; ffmpeg -y -i input.foo -c:v libx264 -profile:v high -level 5 -preset slow -b:v $videobitrate -b:a $audiobitrate -pass 2 output.bar
libx264 is the h264 encoder, and all of the options get passed from ffmpeg to it, they're pretty self explanatorah. The first pass doesn't need audio, hence the -an. You can pipe the output to /dev/null if you want, but whateverrr. I just use the -y switch in the second pass so it overwrites the temp file without asking. The audio codec by default is aac so you don't have to specify it.
ffprobe can help you to get the value of $videobitrate and $audiobitrate (I'm assuming a posix environment, otherwise it's going to be %videobitrate% and %audiobitrate%). You're going to have to do some sed, awk or perl voodoo to get the values into a form that you can use. Here's the output of ffprobe on a random mp4 on my machine:
ffprobe version 2.1.3 Copyright (c) 2007-2013 the FFmpeg developers built on Feb 12 2014 22:10:38 with Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) configuration: --prefix=/usr/local/Cellar/ffmpeg/2.1.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid --enable-libfreetype --enable-libtheora --enable-libvorbis --enable-libvpx --enable-librtmp --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-aacenc --enable-libass --enable-ffplay --enable-libspeex --enable-libschroedinger --enable-libfdk-aac --enable-openssl --enable-libopus --enable-frei0r --enable-libcaca --enable-libopenjpeg --extra-cflags='-I/usr/local/Cellar/openjpeg/1.5.1/include/openjpeg-1.5 ' libavutil 52. 48.101 / 52. 48.101 libavcodec 55. 39.101 / 55. 39.101 libavformat 55. 19.104 / 55. 19.104 libavdevice 55. 5.100 / 55. 5.100 libavfilter 3. 90.100 / 3. 90.100 libavresample 1. 1. 0 / 1. 1. 0 libswscale 2. 5.101 / 2. 5.101 libswresample 0. 17.104 / 0. 17.104 libpostproc 52. 3.100 / 52. 3.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'loop.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf55.19.104 Duration: 00:00:19.56, start: 0.000000, bitrate: 201 kb/s Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 400x226 [SAR 226:225 DAR 16:9], 129 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 75 kb/s (default) Metadata: handler_name : SoundHandler
annoyingly ffprobe dumps everything to stderr, so you need to redirect it to stdout. I won't tell you how to suck eggs, your sed-fu might be better than mine, but for Those Who Came in Late, to get the bitrate for the whole shebang in the example above you could to do something like:
videobitrate=$(ffprobe input.foo 2>&1|grep bitrate |sed "s/.*bitrate: \([0-9]*\) \([km]*\).*/\1\2/")
and for the audio:
audiobitrate=$(ffprobe input.foo 2>&1|grep Audio|sed "s/.* \([0-9]*\) \([km]*\)b\/s.*/\1\2/")
not sure why the audio's bitrate was specified but not the video's, could be because I usually compress with a constant rate factor (constant quality rather than constant bitrate). You'll have to check the ffprobe output on your movies. From there it's up to you turning that into the variables you need. Note that ffmpeg will parse numbers like 100k or 3m.