timgavin
timgavin

Reputation: 5166

FFProbe to Get Codec

I've tried a bunch of different examples of getting a video's codec using FFProbe, including this one and just can't get any output other than an occasional [/STREAM].

This is what I'm currently trying

$codec = exec("ffprobe -v error -show_entries -show_streams stream=codec_name {$input['filename']}");

Tried this too...

$codec = exec("ffprobe -v quiet -print_format json -show_format -show_streams {$input['filename']}");

I know the video is good and it's working on the CLI, because when I use the following to get the duration I get the expected result

$duration = exec("ffprobe {$input['filename']} -show_format 2>&1 | sed -n 's/duration=//p'");

Any ideas?

Upvotes: 4

Views: 9528

Answers (2)

Rusty Nail
Rusty Nail

Reputation: 2710

A great answer from timgavin, Thank You!

My requirement meant I needed a little different application, I used Command Prompt:

set probe="<YOUR PATH>/ffmpeg/bin/ffprobe.exe"
%probe% -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.avi

which in this case, returns:

mpeg4

Not sure this will be enough to for fill my requirements. HTML5 Video Tag is a poorly documented standard from my current research?

Thanks to @llogan comment, and more research, I have decided to post what I have found on the Video Tag:

ffmpeg/bin/ffmpeg.exe" -i input.avi -b 1500k -vcodec libx264 "output.mp4"

or

ffmpeg/bin/ffmpeg.exe -i input.mov -vcodec h264 -acodec aac -strict -2 output.mp4

Of course, the:

h264

Video codec is the only codec of the .mp4 Mime File Type that is supported. This guide may be of use: https://trac.ffmpeg.org/wiki/Encode/H.264

As far as I can tell, if the Browser one is using, is up to date, it should support these Video Mime Types!

Thanks to @llogan 's advice, we no longer need:

-strict -2

but use:

-movflags +faststart

What I have now, thanks to lloyd's help and research is:

string ffmpeg = "Utils/ffmpeg/ffmpeg.exe";
string encode = "-y -i \"" + InputFile + "\" -movflags +faststart -vcodec libx264 -crf 22 -acodec aac -b:a 192k \"" + OutputFile + "\"";

Where: OutputFile is OutputFile.mp4

As of: 06.12.2020 this is a good ffmpeg encoding for the HTML5 Video Tag.

Upvotes: 1

timgavin
timgavin

Reputation: 5166

Figured it out.

$codec = exec("ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 {$input['filename']}");

echo $codec;

produces

h264

Upvotes: 11

Related Questions