Page 1 of 1
How to route ffmpeg output to text file? [SOLVED]
Posted: Fri Aug 25, 2023 9:27 am
by greengeek
Could not really decide where this question should go - but hope programming is a suitable place.
I need to view the text output from an ffmpeg query.
I have tried using the -report argument and yes it does print a .log file with a name such as "ffmpeg-20230825-211808.log"
However - the long file name and timestamp can be problematic for me to handle in a script. I would really like to force the ffmpeg code to re-route the text output to a text file with a name that I can specify.
Then I hope to grep that file for certain terms.
Or - even better - if at all possible I would like to directly grep the terms straight from the ffmpeg output.
The ffmpeg query and response is such as this:
Code: Select all
root# ffmpeg -report -f v4l2 -list_formats all -i /dev/video0
ffmpeg started on 2023-08-25 at 21:24:36
Report written to "ffmpeg-20230825-212436.log"
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7.3.0 (GCC)
configuration: --prefix=/usr --libdir=/usr/lib64 --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-pthreads --enable-small --enable-postproc --enable-libvorbis --enable-gpl --enable-shared --enable-nonfree --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-debug --enable-bzlib --enable-zlib --enable-libspeex --enable-version3 --enable-runtime-cpudetect --enable-libxcb --enable-libtheora --enable-libxvid --enable-swscale --enable-libvpx
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
[video4linux2,v4l2 @ 0x1be9fe0] Raw : yuyv422 : YUYV 4:2:2 : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
[video4linux2,v4l2 @ 0x1be9fe0] Compressed: mjpeg : Motion-JPEG : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
/dev/video0: Immediate exit requested
root#
The data I am really interested in is:
Raw : yuyv422 : YUYV 4:2:2 : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
and:
Compressed: mjpeg : Motion-JPEG : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
I have tried redirecting to a text file like this:
root# ffmpeg -report -f v4l2 -list_formats all -i /dev/video0 > formats.txt
- but the created file is empty.
Any thoughts appreciated!
Re: How to route ffmpeg output to text file?
Posted: Fri Aug 25, 2023 11:25 am
by MochiMoppel
greengeek wrote: ↑Fri Aug 25, 2023 9:27 am
I have tried redirecting to a text file like this:
root# ffmpeg -report -f v4l2 -list_formats all -i /dev/video0 > formats.txt
- but the created file is empty.
Any thoughts appreciated!
ffmpeg -report -f v4l2 -list_formats all -i /dev/video0 2> formats.txt
Or - even better - if at all possible I would like to directly grep the terms straight from the ffmpeg output.
The data I am really interested in is:
Raw : yuyv422 : YUYV 4:2:2 : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
and:
Compressed: mjpeg : Motion-JPEG : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
ffmpeg -report -f v4l2 -list_formats all -i /dev/video0 2>&1 | grep -Eo '(Raw|Compressed).*'
[Edit]:Changed grep command to create just the lines greengeek is interested in
Re: How to route ffmpeg output to text file?
Posted: Fri Aug 25, 2023 1:44 pm
by Burunduk
MochiMoppel has proposed an easy and universal way of redirecting the stderr that will work with other commands too, but if you want to change the ffmpeg report file name, this is possible to do by setting an environment variable:
export FFREPORT=file=/path/to/your/ffmpeg-report.txt
[ffmpeg set report log filename]
Re: How to route ffmpeg output to text file?
Posted: Fri Aug 25, 2023 9:17 pm
by greengeek
MochiMoppel wrote: ↑Fri Aug 25, 2023 11:25 am
ffmpeg -report -f v4l2 -list_formats all -i /dev/video0 2> formats.txt
Ahhh, thank you. Much obliged.
Note to self for future reference:
Programs such as ffmpeg output more than one data stream:
Some may be non-text and will go to stdout
Some may be actual text and will go to stderr. (Seems like a misnomer to me as genuine non-error messages will also go to stderr so maybe it should be called stdmsgs instead)
Using the "2>" redirect Mochi shows above causes the script to look at the "err" (or "text messages") part of the ffmpeg output.
Using the "1>" redirect (which i guess is the default when just ">" is used) will cause the script to handle the stdout portion which is non text and socant be saved to a text file.
Apologies if this is inaccurate - it's just my current understanding and will help me next time i encounter the same issue ![Smile :-)](./images/smilies/icon_e_smile.gif)
Re: How to route ffmpeg output to text file?
Posted: Fri Aug 25, 2023 9:28 pm
by greengeek
Burunduk wrote: ↑Fri Aug 25, 2023 1:44 pm
if you want to change the ffmpeg report file name, this is possible to do by setting an environment variable:
export FFREPORT=file=/path/to/your/ffmpeg-report.txt
Thank you. Is an environment variable something that would be set within a bash script or is it set within some /root/.config file please?
Re: How to route ffmpeg output to text file?
Posted: Fri Aug 25, 2023 11:34 pm
by Burunduk
greengeek wrote: ↑Fri Aug 25, 2023 9:28 pm
Is an environment variable something that would be set within a bash script or is it set within some /root/.config file please?
You could do either: add this export statement to the /etc/profile file for persistence - this way the variable will be available for all the scripts you run; or set it in the script - this way you can use different names for different scripts or even for different commands:
Code: Select all
export FFREPORT=file=/root/report.txt
ffmpeg -report -f v4l2 -list_formats all -i /dev/video0
Or add the variable only to the ffmpeg command environment (only this sole command will see it):
Code: Select all
FFREPORT=file=/root/report.txt ffmpeg -report -f v4l2 -list_formats all -i /dev/video0
it's just my current understanding and will help me next time i encounter the same issue
Summary:
command >file.txt
(yes, 1> is the same) redirect stdout to file.txt
command 2>file.txt
redirect stderr to file.txt
command >file.txt 2>&1
redirect both stdout and stderr to file.txt (note the order) - ffmpeg can actually produce both streams, so this variant may be the one you need.
https://devhints.io/bash - the Redirection chapter
Re: How to route ffmpeg output to text file? [SOLVED]
Posted: Sun Aug 27, 2023 1:04 am
by MochiMoppel
greengeek wrote: ↑Fri Aug 25, 2023 9:27 amThe data I am really interested in is:
Raw : yuyv422 : YUYV 4:2:2 : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
and:
Compressed: mjpeg : Motion-JPEG : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
@greengeek Sorry, I missed this part of your post. I have edited my reply with a modified grep command. Should result in 2 lines:
Code: Select all
Raw : yuyv422 : YUYV 4:2:2 : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
Compressed: mjpeg : Motion-JPEG : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
Re: How to route ffmpeg output to text file? [SOLVED]
Posted: Sun Aug 27, 2023 7:08 am
by greengeek
MochiMoppel wrote: ↑Sun Aug 27, 2023 1:04 am
Sorry, I missed this part of your post. I have edited my reply with a modified grep command. Should result in 2 lines:
Code: Select all
Raw : yuyv422 : YUYV 4:2:2 : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
Compressed: mjpeg : Motion-JPEG : 640x400 640x480 320x240 320x200 160x120 1280x800 1280x720
Thank you very much for this.
Based on your original answer (and some other googling) I had come up with the following bash oneliner that I was inserting into a gtkdialog gui:
Code: Select all
x=$(ffmpeg -f v4l2 -list_formats all -i /dev/video"$USBCAM" 2>&1 | grep -Eo 'Raw.*$'); gxmessage "$x"
- as a means of advising a user of the "Raw" stream capabilities of their usb webcam.
I had to do this twice (with small tweak) to let the user know of the "Compressed" stream capability also.
So now I will be able to modify my gui to give them both "Raw" & "Compressed" at the same time.
Many thanks!
Appreciate the followup.
Cheers!
Re: How to route ffmpeg output to text file? [SOLVED]
Posted: Sun Aug 27, 2023 7:43 am
by MochiMoppel
greengeek wrote: ↑Sun Aug 27, 2023 7:08 amBased on your original answer (and some other googling) I had come up with the following bash oneliner that I was inserting into a gtkdialog gui:
Code: Select all
x=$(ffmpeg -f v4l2 -list_formats all -i /dev/video"$USBCAM" 2>&1 | grep -Eo 'Raw.*$'); gxmessage "$x"
Since gxmessage allows input from stdin you can make this faster and simpler:
Code: Select all
ffmpeg -f v4l2 -list_formats all -i /dev/video"$USBCAM" 2>&1 | grep -Eo '(Raw|Compressed).*' | gxmessage -c -fn mono -file -
I added the the -c
and -fn mono
options to center the dialog and make it more readable with a monospace font but of course you can leave this out if you don't like it.
Re: How to route ffmpeg output to text file? [SOLVED]
Posted: Sun Aug 27, 2023 8:36 am
by greengeek
MochiMoppel wrote: ↑Sun Aug 27, 2023 7:43 am
Since gxmessage allows input from stdin you can make this faster and simpler:
Code: Select all
ffmpeg -f v4l2 -list_formats all -i /dev/video"$USBCAM" 2>&1 | grep -Eo '(Raw|Compressed).*' | gxmessage -c -fn mono -file -
I added the the -c
and -fn mono
options to center the dialog and make it more readable with a monospace font but of course you can leave this out if you don't like it.
That's awesome - thanks very much.
Took me a while to work with this as I initially thought I needed to graft it into the "x=$..." syntax. But no - it's a standalone that works as is.
Brilliant!
Have grafted this into my gui and it's a very nice addition thank you.
![format_display.jpg](./download/file.php?id=18366&sid=b54d81ff0ded440dd78e8153ab76f4fa)
- format_display.jpg (12.77 KiB) Viewed 1789 times
Re: How to route ffmpeg output to text file? [SOLVED]
Posted: Wed Aug 30, 2023 9:45 am
by greengeek
I am not sure I fully understand how this is working so here is a supplementary question please:
If I issue the following syntax:
...I get a text file with meaningful contents. (showing the formats that ffmpeg understands)
However, if I issue the following syntax (to find out what formats the webcam understands):
Code: Select all
ffmpeg -list_formats all -i /dev/video0 > vid0formats.txt
...I get a text file with blank contents.
What is the difference? Why does one produce valid output and the other not?
If I modify the second syntax as follows:
ffmpeg -list_formats all -i /dev/video0 2> vid0formats.txt
...the text file contains the data that I was looking for.
![Confused :?](./images/smilies/icon_e_confused.gif)
Re: How to route ffmpeg output to text file? [SOLVED]
Posted: Wed Aug 30, 2023 1:37 pm
by MochiMoppel
greengeek wrote: ↑Wed Aug 30, 2023 9:45 am
If I issue the following syntax:
...I get a text file with meaningful contents. (showing the formats that ffmpeg understands)
However, if I issue the following syntax (to find out what formats the webcam understands):
Code: Select all
ffmpeg -list_formats all -i /dev/video0 > vid0formats.txt
...I get a text file with blank contents.
What is the difference? Why does one produce valid output and the other not
As Burunduk already pointed out, ffmpeg uses both streams. In general and very broadly most stuff that is written to the (terminal) screen is written to stderr while the conversion output goes to stdout...or both
:
Example: Your command ffmpeg -formats > formats.txt
produces more output when you run it in a terminal: ffmpeg -formats
. Notice the copyright and configuration info at the top. It is written to stderr while the rest goes to stdout. In order to get the same output on screen as was written to your file formats.txt you would have to suppress stderr: ffmpeg -formats 2>/dev/null
. Or you can prevent the copyright/configuration info by using the -hide_banner
option.
Yes, it can be very confusing, but using stderr is actually pretty clever.
Think of a simple command like ffmpeg -i foo.wma foo.mp3
. This will show "live" conversion info on your terminal (stderr) and at the same time it will write the new file foo.mp3 to stdout. You can force the terminal info to stdout but then it will have to share this channel with the audio file you are creating. Possible but normally not what people want, at least not as the standard behavior.