Add Date and Time to Each Line in ffmpeg Log with awk

When transcode video or catch bugs in live stream, sometimes it is necessary to log ffmpeg messages with also date and time. This is possible with awk.

 

Insert date and time to ffmpeg log messages using awk

Here we use test stream link from [3]. We run ffplay for check its work:

ffplay https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/master.m3u8 2>&1 | awk 'NF {print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush()}'

Explanation:

  • `2>&1` means output errors to standard output
  • `| awk` means pipe the output of ffplay and process text with awk
  • `awk 'NF..` means use only not empty lines
  • 'NF {print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush()}'
    it is a short awk program that use only not empty lines, prints the date, next prints the ffplay message as it is '$0', and next it flushes cached data (that is computed date and time) for use another date and time next time for the next line.

Now let's transcode 40 seconds of this stream to file 1.mp4 and log it to ~/ffmpeg.log

ffmpeg -i https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/master.m3u8 -to 00:00:40 -c:v copy -c:a copy 1.mp4 2>&1 | awk 'NF {print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush()}' > ~/ffmpeg.log

To view the log during the process run tail -f in another terminal:

tail -f ~/ffmpeg.log

2022-08-03 20:52:41 [hls @ 0x560ffa894280] Skip ('#EXT-X-VERSION:3')
2022-08-03 20:52:41 [https @ 0x560ffb9f3640] Opening 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/segment_3_20220…' for reading
[https @ 0x560ffab7fd00] Opening 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/level_0.m3u8' for reading  
2022-08-03 20:52:47 [hls @ 0x560ffa894280] Skip ('#EXT-X-VERSION:3')
2022-08-03 20:52:47 [https @ 0x560ffae8a780] Opening 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/segment_0_20220…' for reading
2022-08-03 20:52:47 [https @ 0x560ffaba0d80] Opening 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/segment_0_20220…' for reading
[https @ 0x560ffab7fd00] Opening 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/level_3.m3u8' for reading  
2022-08-03 20:52:48 [hls @ 0x560ffa894280] Skip ('#EXT-X-VERSION:3')
2022-08-03 20:52:48 [https @ 0x560ffbfd59c0] Opening 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/segment_3_20220…' for reading
2022-08-03 20:52:48 frame=  961 fps= 44 q=-1.0 Lsize=   20779kB time=00:00:39.99 bitrate=4255.7kbits/s speed=1.82x    
2022-08-03 20:52:48 video:20470kB audio:287kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.105126%

As you can see, log has date and time, thanks to awk [3].

 

Use extended ffmpeg log

There is an option -loglevel that can have various settings:

-loglevel [flags+]loglevel | -v [flags+]loglevel

quiet, panic, fatal, error, warning, info, verbose, debug, trace

For example, -loglevel verbose:

ffplay -loglevel verbose https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/master.m3u8 2>&1 | awk 'NF {print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush()}'

 

For more ffmpeg options visit link [1], for more awk examples link [2].

 

Links

1. ffmpeg -loglevel https://ffmpeg.org/ffmpeg.html#toc-Generic-options

2. The GNU AWK User's Guide https://www.gnu.org/software/gawk/manual/gawk.html

2. Free HLS m3u8 URLs for Testing HLS Players https://ottverse.com/free-hls-m3u8-test-urls/

Section
Category