Python中秒、毫秒及视频内时间转epoch方法,时间戳相加有效性验证
1. Converting Seconds/Milliseconds to Epoch & Adding to Another Epoch
First, let’s clarify: epoch timestamps are typically measured either in seconds since 1970-01-01 UTC, or milliseconds (common in systems like JavaScript or many APIs). Here’s how to handle conversions and additions:
- For a duration in seconds:
- If your base epoch timestamp uses seconds, you can directly use this duration as a delta value.
- If your base epoch uses milliseconds, multiply the seconds by 1000 to get the delta in milliseconds.
- For a duration in milliseconds:
- If your base epoch uses milliseconds, use the value directly as a delta.
- If your base epoch uses seconds, divide the milliseconds by 1000 to get the delta in seconds.
Is adding the duration to another epoch timestamp correct?
Yes—but only if the units match. For example:
- If you have an epoch timestamp in seconds (e.g.,
1620000000), adding a 30-second duration gives1620000030, which is the correct epoch for 30 seconds later. - If your epoch is in milliseconds (e.g.,
1620000000000), adding 30000 ms (30 seconds) is correct—but adding 30 (raw seconds) would be wrong (you’d only add 30 ms instead of 30 seconds). Always align units first!
2. Converting Video Duration String to Epoch & Adding to Start Time
Let’s break this down step by step for your specific case:
The string "00:00:45.0010796" is a duration (hours:minutes:seconds.microseconds), not an absolute time. The video start time "1512992049819" is an epoch timestamp in milliseconds. We need to convert the duration to milliseconds, then add it to the start time.
Step 1: Parse the duration string into total milliseconds
Use Python’s datetime.timedelta to parse and convert the duration:
from datetime import timedelta duration_str = "00:00:45.0010796" # Split into hours, minutes, and seconds components h, m, s = duration_str.split(':') # Split seconds into whole seconds and microseconds (truncate to 6 digits, since timedelta uses microseconds) s_whole, s_micro = s.split('.') s_micro = s_micro[:6] # Create a timedelta object representing the duration delta = timedelta( hours=int(h), minutes=int(m), seconds=int(s_whole), microseconds=int(s_micro) ) # Convert to total milliseconds total_ms = delta.total_seconds() * 1000 # Result: ~45001.079 ms
Step 2: Add to the video start epoch
Convert the start epoch string to an integer, then add the duration in milliseconds:
start_epoch_ms = int("1512992049819") final_epoch_ms = start_epoch_ms + total_ms # Or round to whole milliseconds if needed: final_epoch_ms_rounded = start_epoch_ms + round(total_ms)
Is this result a correct epoch timestamp?
Absolutely! Since we converted the duration to the same unit (milliseconds) as the start epoch, adding them gives the absolute epoch timestamp for that exact moment in the video. This works because we’re effectively calculating "video start time + time elapsed in video" to get the real-world absolute time.
内容的提问来源于stack exchange,提问作者Tasos




