You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

时间码数组差值计算及FFmpeg适配需求

Alright, let's work through this problem since you need to calculate timecode differences for FFmpeg compatibility, plus extract specific timecodes for FFmpeg input. Here's a straightforward, practical solution:

Timecode Difference Calculation & Extraction for FFmpeg

Core Approach

To solve this, we'll:

  1. Convert FFmpeg-style timecodes (HH:MM:SS.mmm) to numeric values (total seconds) for easy arithmetic.
  2. Calculate the difference between each pair of timecodes.
  3. Convert the difference back to the standard FFmpeg timecode format.
  4. Extract the first timecode from each pair into a separate array for FFmpeg input.

Helper Functions for Timecode Conversion

First, we need two helper functions to handle converting between timecode strings and numeric seconds—this is the backbone of the solution.

def timecode_to_seconds(timecode):
    """Convert HH:MM:SS.mmm timecode to total seconds as a float"""
    hours, minutes, rest = timecode.split(':')
    seconds, milliseconds = rest.split('.')
    total_seconds = (int(hours) * 3600) + (int(minutes) * 60) + int(seconds) + (int(milliseconds) / 1000)
    return total_seconds

def seconds_to_timecode(total_seconds):
    """Convert total seconds back to HH:MM:SS.mmm format (FFmpeg-compatible)"""
    hours = int(total_seconds // 3600)
    remaining = total_seconds % 3600
    minutes = int(remaining // 60)
    seconds_raw = remaining % 60
    seconds = int(seconds_raw)
    milliseconds = int((seconds_raw - seconds) * 1000)
    # Ensure leading zeros and 3-digit milliseconds
    return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"

Full Processing Code

Now, let's put it all together to process your input array of timecode pairs:

# Example input array (each element is a timecode pair separated by ' - ')
input_timecode_pairs = [
    "00:00:25.600 - 00:00:22.180",
    "00:00:27.790 - 00:00:24.070"
]

# Initialize arrays for results
difference_timecodes = []
ffmpeg_input_timecodes = []

for pair in input_timecode_pairs:
    # Split the pair into individual timecodes
    start_tc, end_tc = pair.split(' - ')
    
    # Collect the first timecode for FFmpeg input
    ffmpeg_input_timecodes.append(start_tc)
    
    # Calculate the time difference
    start_sec = timecode_to_seconds(start_tc)
    end_sec = timecode_to_seconds(end_tc)
    diff_sec = start_sec - end_sec  # Swap to end_sec - start_sec if you need reverse order
    
    # Convert difference back to FFmpeg-compatible timecode
    diff_tc = seconds_to_timecode(diff_sec)
    difference_timecodes.append(diff_tc)

# View the results
print("Calculated time differences (FFmpeg format):", difference_timecodes)
print("First timecodes for FFmpeg input:", ffmpeg_input_timecodes)

Sample Output

Running the code above will give you:

Calculated time differences (FFmpeg format): ['00:00:03.420', '00:00:03.720']
First timecodes for FFmpeg input: ['00:00:25.600', '00:00:27.790']

Key Notes for FFmpeg Compatibility

  • The output timecodes strictly follow the HH:MM:SS.mmm format, which works seamlessly with FFmpeg parameters like -ss (start time), -t (duration), or -to (end time).
  • If your timecode pairs might have the end time earlier than the start (resulting in a negative difference), wrap the calculation in abs() to get a positive duration: diff_sec = abs(start_sec - end_sec).
  • If your input timecodes have variations (e.g., missing leading zeros, fewer than 3 millisecond digits), adjust the helper functions to handle those cases (e.g., pad milliseconds with zeros).

内容的提问来源于stack exchange,提问作者tjallo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:22:08