【问题标题】:Calculate two audio duration with frame用帧计算两个音频持续时间
【发布时间】:2023-03-09 13:00:02
【问题描述】:

我卡住了,我需要一个小逻辑。

我有两个音频持续时间

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

H=小时 S= 秒 M=分钟 F=帧

我的问题是

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

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

如果帧大于 30,它应该在秒内增加 1。

我正在使用电源外壳。如何确定计算这两者的逻辑?

【问题讨论】:

  • 这些音频持续时间 - 它们来自哪里?它们只是字符串吗? [timespan] 可能是?
  • @MathiasR.Jessen tks 供您快速回放,是的,它们是字符串,此持续时间取自 ffmpeg - ffprobe 我需要添加两个持续时间
  • 帧率是30还是31?由于第一个示例中的 :00 帧数而询问
  • @MathiasR.Jessenyes 30 岁,我已经编辑了问题,抱歉。

标签: powershell logic duration calculation


【解决方案1】:

计算前 3 部分(hour:minute:second),我们可以卸载到[timespan] 类型,那么我们需要担心的就是携带多余的帧:

# 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
}

现在我们可以这样做了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多