使用SQL时间码计算帧

时间:2019-04-05 15:09:28

标签: sql sql-server

所以我有一个如下的SQL表

+---------+-------------+-------------+------------------+-----------------+
| Barcode |    Time1    |    Time2    | Frame for time 1 | Frame for time2 |
+---------+-------------+-------------+------------------+-----------------+
|       1 | 10:20:34.25 | 23:59:59.25 | NULL             | NULL            |
|         |             |             |                  |                 |
+---------+-------------+-------------+------------------+-----------------+

我想要一种方法来计算Time1和Time2的帧,如以下网站所做的http://www.zapstudio.net/framecalc/所示,以产生下表

+---------+-------------+-------------+------------------+-----------------+
| Barcode |    Time1    |    Time2    | Frame for time 1 | Frame for time2 |
+---------+-------------+-------------+------------------+-----------------+
|       1 | 10:20:34.25 | 23:59:59.25 | 930880           | 2160000         |
|         |             |             |                  |                 |
+---------+-------------+-------------+------------------+-----------------+

但是,在计算帧期间,我希望从另一个表中读取每个条形码的帧率,

+---------+--------+------------+
| Barcode | Format | Frame Rate |
+---------+--------+------------+
|       1 | 1080p  |         25 |
+---------+--------+------------+

2 个答案:

答案 0 :(得分:1)

一旦您了解了帧速率就是每秒的帧数这一事实,这就是一个简单的计算。您只需要将时间转换为秒,然后在看似小数点之后添加值即可,但这只是其他帧。 以下计算使用时间隐式转换,但仅限于23:59:59。如果需要更大的值,可以设置更复杂的转换。它还不考虑格式错误。

CREATE TABLE #Barcodes(
    Barcode         int,
    Time1           varchar(11),
    Time2           varchar(11),
    FramesForTime1  int,
    FramesForTime2  int
)
INSERT INTO #Barcodes(Barcode, Time1, Time2)
VALUES( 1, '10:20:34.25', '23:59:59.25')
CREATE TABLE #FrameRates(
    Barcode         int,
    Format          varchar(11),
    FrameRate       int
)
INSERT INTO #FrameRates VALUES( 1, '1080p', 25);

SELECT bc.Barcode, 
    bc.Time1,
    bc.Time2,
    DATEDIFF( ss, 0, LEFT( Time1, 8)) * Fr.FrameRate + RIGHT(Time1, 2) AS FramesForTime1, 
    DATEDIFF( ss, 0, LEFT( Time2, 8)) * Fr.FrameRate + RIGHT(Time2, 2) AS FramesForTime2,
    fr.Format,
    fr.FrameRate
FROM #Barcodes bc
JOIN #FrameRates fr ON bc.Barcode = fr.Barcode

GO
DROP TABLE #Barcodes, #FrameRates

我同意建议不要存储这些值的评论。但是,您可以创建将在需要时即时计算的计算列。

答案 1 :(得分:1)

数学公式为:([小时] * 3600 + [分钟] * 60 + [秒])* [帧速率] + [毫秒]

这是更新表的代码:

CREATE TABLE #FramesConfig
    (
        Barcode int,
        Format varchar(8),
        FrameRate int
    )

CREATE TABLE #Frames
(
    Barcode int,
    Time1 time(2),
    Time2 time(2),
    FrameForTime1 int,
    FrameForTime2 int
)

INSERT INTO #FramesConfig
VALUES(1, '1080p', 25)

INSERT INTO #Frames
VALUES (1, '10:20:34.25', '23:59:59.25', NULL, NULL)

UPDATE f
SET FrameForTime1 = DATEDIFF(SS, 0, f.Time1) * fc.FrameRate + RIGHT(f.Time1, 2),
FrameForTime2 = DATEDIFF(SS, 0, f.Time2) * fc.FrameRate + RIGHT(f.Time2, 2)
FROM #Frames f
    INNER JOIN #FramesConfig fc ON f.Barcode = fc.Barcode


SELECT *
FROM #Frames

DROP TABLE #Frames
DROP TABLE #FramesConfig