I wrote the following wrapper for FFMPEG:
function Video($input, $crop = null, $scale = null, $output = null, $extra = null)
{
$input = @new ffmpeg_movie
You want to look for the Shannon-Entropy -log(P)/log(2). This is the minimum bits any information can be think of. But I'm unsure if it is useful to you.
I ended up using the -sameq flag, I read somewhere that this doesn't translate to the same quality but for now this is better than forcing the original bit rate.
Anyway, I've come across this Bash script that suggests that my thinking is right, I still don't know how to calculate the output bit rate without having the output size as a constrain. If anyone knows, please share!
In general you shouldn't specify a bitrate at all. It's only useful for streaming, in which case you need to respect VBV as well (which specifies a maximum bitrate over time, as well as the average bitrate).
Use x264 crf 23 - its default constant-quality mode- and be happy. In the case of ffmpeg, this is something like:
ffmpeg -i <file> -vcodec libx264 -vpre slower -acodec copy <outfile>
As for audio, it's best directly copied if the input was compressed. This is not possible in some situations, such as if the input was vorbis and the output is a .flv file. In that case I would stick to whatever the default of the audio encoder selected is.