Redirecting Ffmpeg Result into Another File

I'm trying to get the size of an input video using ffmpeg, below is the code that I use, what I'm trying to do is to first store the result into a txt file and then do some parsing to get the size of the video:

$ ffmpeg -i TheNorth.mp4 

The terminal says "At least one output file must be specified" Then I tried this:

$ ffmpeg -i TheNorth.mp4 result.txt

The terminal says "Unable to find a suitable output format for 'size.txt'"

So how could I get the result and save it to the specified file?

3 Answers

You can store the output ffmpeg generates with piping:

ffmpeg -i TheNorth.mp4 2> result.txt

You need to use 2> here, as ffmpeg writes to StdErr (and not StdOut).

1

ffprobe

If you just want to get the size of the video then you can get that, and other info, directly with ffprobe. This will avoid redirection, temporary output files, and the additional parsing.

$ ffprobe -v error -select_streams v:0 -show_entries stream=height,width -of csv=p=0:s=x input.mkv 
  1280x720

See FFmpeg Wiki: FFprobe Tips for more examples.

tee

For users who want to encode and capture the resulting console output, I recommend using tee. The problem with pure redirection is that important messages such as error messages, failures, and prompts can be missed.

You can avoid this by including tee to show the output in the console and to save it to a file:

ffmpeg -i input … output |& tee console.txt

ffmpeg outputs to stderr instead of the more typical stdout, so the & is added to the | pipe to deal with that. This is only for Bash 4+. If you're using something else then change |& to 2>&1 which redirects stderr to stdout before it is sent to the pipe.

3

Somewhat better idea is to use ffprobe

ffprobe -show_format -print_format json TheNorth.mp4

that will output JSON formated info about video. Guess it is easier to parse than raw output. To redirect output to file use just ordinary pipe > result.txt similar to accepted answer but without two.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest