我有一個 YouTube 視頻的時間戳串列。
例子:
timestamps = ['0:02-0:35', '0:50-1:18', '2:53-3:08', '3:12-3:14', '3:16-3:22', '3:25-3:28', '1:09-1:35', '1:38-1:48', '2:04-2:14', '2:30-2:40', '2:45-2:50', '3:35-4:07', '4:16-4:22', '4:48-4:54', '5:00-5:12', '5:34-5:54', '8:58-9:19', '']
現在我需要為每個時間戳獲取starttimeand ,因為我需要將這些作為引數傳遞給將視頻剪切成子剪輯的方法。endtimesecondsffmpeg_extract_subclip
例如,
# If the timestamp is 0:02-0:35 The starttime and endtime should be,
timestamp = '0:02-0:35'
starttime = 2
endtime = 35
# If the timestamp is 3:02-3:35 The starttime and endtime should be,
timestamp = '3:02-3:35'
starttime = 182 # [(3 * 60) 2]
endtime = 215 # [(3 * 60) 35]
我們可以用正則運算式嗎?或者還有其他選擇嗎?
uj5u.com熱心網友回復:
您可以使用正則運算式來執行此操作,從源字串中捕獲分鐘 ( \d{1,2}) 和秒 ( )。\d{2}
import re
interval = '0:02-0:35'
start_min, start_sec, end_min, end_sec = map(int, re.findall('\d{1,2}', interval))
:或通過和-通過分割時間re.split。
start_min, start_sec, end_min, end_sec = map(int, re.split('[:-]', interval))
但是您也可以通過內置函式簡單地做到這一點str.split。
start_min, start_sec, end_min, end_sec = (int(t) for time in interval.split('-') for t in time.split(':'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/532264.html
上一篇:我想知道每十年拍了多少部電影
