FFMPEG:将当前时间(以毫秒为单位)嵌入到视频中 [英] FFMPEG: embed current time in milliseconds into video
问题描述
我想使用FFMPEG将计算机的本地时间 以毫秒为单位 嵌入流中.
I would like to embed the computer's local time in milliseconds into a stream using FFMPEG.
我可以像这样使用drawtext嵌入本地时间(以秒为单位):
I can embed the local time in seconds using drawtext like so:
ffmpeg -i <input> -vf "drawtext=text='%{localtime\:%T}'" -f flv <output>
仔细阅读有关drawtext的文档,进行一些测试以及四处搜索,看来localtime
和gmtime
并没有提供毫秒-只是几秒钟.但是pts
选项 确实 有毫秒:
Looking through the documentation for drawtext, doing some tests, and Googling around, it seems like localtime
and gmtime
don't provide milliseconds -- just seconds. However the pts
options does have milliseconds:
ffmpeg -i <input> -vf "drawtext=text='%{pts\:hms}'" -f flv <output>
我还发现pts
支持偏移量,因此我可以使用类似的东西来显示本地时间(种类):
I also found out that pts
supports an offset, so I was able to use something like this to display local time (kind of):
ffmpeg -i <input> -vf "drawtext=text='%{pts\:hms\:$(date +%s.%N)}'" -f flv <output>
这有两个问题:
- 当时显示的内容类似于
17499:17:29
...我发现了一种(相当复杂的)解决方法 - 它使用服务器第一次运行ffmpeg命令时的当前时间作为偏移量,而不是ffmpeg实际开始解码视频时服务器的当前时间.我注意到ffmpeg的启动时间大约为2秒,导致时钟大约关闭了2秒
- It displayed something like
17499:17:29
for the time... I found a (rather complex) way to work around this - It uses the server's current time when you first run the ffmpeg command as the offset -- not the server's current time when ffmpeg actually starts decoding video. I noticed ffmpeg had about a 2-second startup time, causing the clock to be off by roughly 2 seconds
是否可以修改我的解决方案或完全独立的解决方案,以将服务器本地时间(以毫秒为单位)嵌入流中?
Is there a way to modify my solution, or an entirely separate solution, that can embed server local time in milliseconds into the stream?
推荐答案
使用
ffmpeg -i input
-vf "settb=AVTB,
setpts='trunc(PTS/1K)*1K+st(1,trunc(RTCTIME/1K))-1K*trunc(ld(1)/1K)',
drawtext=text='%{localtime}.%{eif\:1M*t-1K*trunc(t*1K)\:d}'"
-f flv out
首先,通过将时基强制为默认的AVTB 10e-6
,将传入的时间戳表示为微秒精度.
First, the incoming timestamps are represented as microsecond precision by forcing the timebase to the default AVTB which is 10e-6
.
第二,设置一个新的PTS,其中包括减少到毫秒并向左移(十进制)三位数的原始PTS.对此,添加了挂钟的毫秒部分. RTCTIME有setpts可用,并返回一个微秒精度的整数.
Second, a new PTS is set, which comprises of the original PTS reduced to milliseconds and left-shifted (decimally) three digits. To this, is added the wallclock's milliseconds component. RTCTIME is available in setpts and returns an integer with microsecond precision.
第三,文本字符串分为三个部分.第一个是本地时间,格式为Y-M-D-H-M-S字符串,并返回第二个精度.然后是一个点,以毫秒为单位.在第三个中,t
返回小数秒.提取毫秒分量并将其打印为整数.
Third, the text string has three parts. The first is the localtime which is formatted as a Y-M-D-H-M-S string and returns second precision. Then a dot to demarcate the milliseconds. In the third, t
returns fractional seconds. The millisecond component is extracted and printed as an integer.
这篇关于FFMPEG:将当前时间(以毫秒为单位)嵌入到视频中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!