18

I'm using this script for shot detection in ffmpeg.

ffprobe -show_frames -of compact=p=0 -f lavfi "movie=test.mp4,select=gt(scene\,0.3)"

I need to write the output into a text file in order to read the output from a c program. How can I do this? Any help is appreciated.

Asanka sanjaya
  • 1,461
  • 3
  • 17
  • 35

1 Answers1

43

You redirect the output to a file:

ffprobe -show_frames -of compact=p=0 -f lavfi "movie=test.mp4,select=gt(scene\,0.3)" > output.txt 2>&1

If you want separate files for stdout and stderr you can do:

[..] > out.txt 2> err.txt

aergistal
  • 29,947
  • 5
  • 70
  • 92
  • Yes, I remember now this. It was always a mystery to me. Is there any explanation for this weird thing? – Apostolos Jan 01 '20 at 17:13
  • @Apostolos Which weird thing? – aergistal Jan 02 '20 at 14:13
  • I mean this `2>&1` stuff. Why, do you have an explanation for it? In **what way** does it solve the problem that the regular `>file` redirection doesn't work? – Apostolos Jan 04 '20 at 06:35
  • 4
    There are two different standard output streams, `stdout` (1) and `stderr` (2) which can be used individually. `>` redirects `stdout` (1) to a file and `2>&1` redirects `stderr` (2) to a copy of the file descriptor used for (1) so both the normal output and errors messages are written to the same file. Some shells have a `&>` to redirect both standard output streams. – aergistal Jan 05 '20 at 10:02
  • Thanks. So, if understand well, '1>' is never used since it's implied (default redirection from `stdout` output). And if just a '>' doesn't work it means that the program -- weirdly enough -- sends output to `stderr`, right? (I won't ask why so! :)) – Apostolos Jan 09 '20 at 09:48
  • @Apostolos it's up to the programmer, you may want to keep normal output separate from error output. Since your top tag is [tag:python], `print("...")` will write to `stdout` and `print("...", file=sys.stderr)` will print to `stderr`. – aergistal Jan 09 '20 at 18:38
  • Well, when you use `stderr` to print "help" for your program at user request, and the text cannot be captured by the user who normally knows only about **regular** redirection, you are not so wise a programmer, are you? Fortunately though, most **are** wise! :)) – Apostolos Jan 11 '20 at 12:01
  • 3
    @Apostolos That's not a good reasoning. In `ffmpeg`'s case stdout might be used for video output in order to pipe it to other processes. – aergistal Jan 13 '20 at 10:00