Calculate two audio duration with frame

北城余情 提交于 2021-01-29 13:20:33

问题


I am stuck, I need a small logic.

I have two audio durations

x = "00:00:07:18"
y = "00:00:06:00"        H : M: S: F  
Answer should be x + y = 00:00:13:18 

H=hours S= seconds M=minutes F=frame

My question is

if x = "00:00:03:14"
   y = "00:00:13:18"

answer should be x + y = **00:00:17:02**

If the frame is greater than 30 it should increase 1 in second.

I am using power shell. How can I determine the logic to calculate both of this?


回答1:


Calculation of the first 3 parts (hour:minute:second), we can offload to the [timespan] type, then all we need to worry about is carrying excess frames over:

# Simple helper function to turn our input strings into a [timespan] + frame count
function Parse-FrameDuration
{
    param(
        [string]$Duration
    )

    $hour,$minute,$second,$frame = ($Duration -split ':') -as [int[]]

    [PSCustomObject]@{
        Time = New-TimeSpan -Hours $hour -Minutes $minute -Seconds $second
        Frame = $frame
    }
}

# function to do the actual calculation
function Add-Frame
{
    param(
        [string]$Base,
        [string]$Offset
    )

    # Parse our two timestamps
    $a = Parse-FrameDuration $Base
    $b = Parse-FrameDuration $Offset

    # Calculate frames % frame rate, remember to carry any excess seconds
    $frames = 0
    $carry = [math]::DivRem($a.Frame + $b.Frame , 30, [ref]$frames)

    # Calculate time difference, add any extra second carried from frame count
    $new = ($a.Time + $b.Time).Add($(New-TimeSpan -Seconds $carry))

    # Stitch output string together from new timespan + remaining frames
    return "{0:hh\:mm\:ss}:{1:00}" -f $new,$frames
}

Now we can do:

PS C:\> Add-Frame -Base 00:00:03:14 -Offset 00:00:13:18
00:00:17:02


来源:https://stackoverflow.com/questions/61236204/calculate-two-audio-duration-with-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!