gpt4 book ai didi

awk - 在重复序列的迭代之前或之后使用 AWK 查找字符串

转载 作者:行者123 更新时间:2023-12-04 22:48:20 24 4
gpt4 key购买 nike

所以我有一个看起来像这样的文本文档(截断)

[FRAME]
pkt_pts_time=0.000000
pict_type=I
[/FRAME]
[FRAME]
pkt_pts_time=0.250250
pict_type=B
[/FRAME]
[FRAME]
pkt_pts_time=0.500500
pict_type=P
[/FRAME]
[FRAME]
pkt_pts_time=0.750750
pict_type=B
[/FRAME]
[FRAME]
pkt_pts_time=0.959292
pict_type=I
[/FRAME]

此文本是使用以下命令创建的:
ffprobe -select_streams v -show_frames -show_entries frame=pkt_pts_time,pict_type,frame_number -v quiet input.mp4

如您所见,重复 [Frame] 到 [/Frame] 序列。所以这是我计算帧数并找出哪个帧是 I 帧的一种方法。在每个序列中,“pict_type=”值都会发生变化。我想知道是否有办法让我使用 AWK 输入迭代次数并输出前面的 pkt_pts_time 值,其中 pict_type 值等于 I。

例如,如果我的帧号是 3。我可以输入数字 3,然后 awk 表达式会转到第三个 [Frame] 到 [/Frame] 序列,然后从那里往回看,直到找到“pict_type=我”字符串。然后它会看到该序列迭代的 pkt_pts_time 是“pkt_pts_time=0.00000”,它会输出 0.0000

最佳答案

检查这个。我会解释它是如何工作的,如果你想要的话。
我通过结束标签来计算帧数 - [/FRAME] , 但可以改为起始标签[FRAME] .

awk -F '=' -v frame_number=3 '
$1 == "[/FRAME]" {
frame_cnt++;
}
$1 == "pkt_pts_time" {
tmp_time = $2;
}
$2 == "I" {
i_time = tmp_time;
}
frame_cnt == frame_number {
print i_time;
exit;
}' input.txt

I 帧后有帧号的版本:
awk -F '=' -v frame_number=3 '
$1 == "[/FRAME]" {
frame_cnt++;
}
$1 == "pkt_pts_time" {
tmp_time = $2;
}
$2 == "I" {
i_time = tmp_time;
i_frame_number = frame_cnt + 1;
}
frame_cnt == frame_number {
print "The I frame time = " i_time;
print "The I frame number + 1 = " i_frame_number + 1;
exit;
}' input.txt

此版本打印最接近目标帧的上下“I”帧值:
awk -F '=' -v frame_number=3 '
# The frame counter - each time the first field of the line
# equals to the [FRAME] string, the counter increments.

$1 == "[FRAME]" {
frame_cnt++;
}
# The "tmp_time" variable is updated each time the "pkt_pts_time" occurs.
# So, it does not have fixed value, it changing each time - floating.

$1 == "pkt_pts_time" {
tmp_time = $2;
}
# Here we are determining the nearest "I" frame, before the target frame.
# It works this way: each time the "I" frame occurs, the "i_lower" value
# updated. It happens, while we are not reach the target frame. Then, it is
# last time, whey the "i_lower" variable is updated. So, we found the nearest
# "I" frame before the target frame.

frame_cnt <= frame_number && $2 == "I" {
i_lower = tmp_time;
}
# Here, we are determining the nearest "I" frame, after the target frame.
# When it occurs, the lower and upper "I" frame values are printed
# and the script execution stops.
# Note, that if the upper "I" frame does not exist, the script will print nothing,
# because, the condition returns false.

frame_cnt >= frame_number && $2 == "I" {
print "lower I = " i_lower;
print "upper I = " tmp_time;
exit;
}' input.txt

关于awk - 在重复序列的迭代之前或之后使用 AWK 查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47105443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com