用python根據(jù)視頻名修改視頻創(chuàng)建時(shí)間

問(wèn)題

用mylio整理照片,發(fā)現(xiàn)它會(huì)按照照片的拍照時(shí)間自動(dòng)對(duì)照片歸類(lèi)。但是,有的視頻的時(shí)間明顯不對(duì),一番折騰后發(fā)現(xiàn)問(wèn)題所在


image.png

很明顯,照片的創(chuàng)建時(shí)間和修改時(shí)間不對(duì),對(duì)比下文件名就知道了,而mylio是按照創(chuàng)建時(shí)間來(lái)歸類(lèi)的

要是一個(gè)文件也就罷了,但是這些年實(shí)在攢了太多視頻了,例如2017年視頻被放到2018年的文件

image.png

還是寫(xiě)個(gè)腳本批量修改吧

解決過(guò)程

搜了很久,網(wǎng)上主要是修改訪問(wèn)時(shí)間atime修改時(shí)間mtime,通過(guò)os.utime(filename,(atime,mtime))方法來(lái)做,但是,沒(méi)有簡(jiǎn)單的修改創(chuàng)建時(shí)間ctime的方法

發(fā)現(xiàn)了這個(gè)
https://github.com/Delgan/win32-setctime

使用pip安裝后,可以使用

from win32_setctime import setctime
setctime("my_file.txt", 1561675987.509)

那串?dāng)?shù)字是epoch time,所以,需要將時(shí)間轉(zhuǎn)換為epoch time,使用time.strptime()time.mktime()即可

測(cè)試下

>>> import time
>>> filename='VID_20170919_223923.mp4'
>>> pattern='VID_%Y%m%d_%H%M%S.mp4'
>>> newtime=time.strptime(filename,pattern)
>>> newtime
Out[7]: time.struct_time(tm_year=2017, tm_mon=9, tm_mday=19, tm_hour=22, tm_min=39, tm_sec=23, tm_wday=1, tm_yday=262, tm_isdst=-1)
>>> newtime_stamp=time.mktime(newtime)
>>> from win32_setctime import setctime
>>> setctime(filename, newtime_stamp)
>>> import os
>>> os.utime(filename,(newtime_stamp,newtime_stamp))

成功!

最終方案

from win32_setctime import setctime
import time
import os
filenames=os.listdir()
for filename in filenames:
    # check whether it has the form VID_....mp4
    filename=filename.strip()
    if filename.startswith('VID') and filename.endswith('.mp4'):
       print(filename)
       pattern='VID_%Y%m%d_%H%M%S.mp4'
       newtime=time.strptime(filename,pattern)
       newtime_stamp=time.mktime(newtime)
       setctime(filename,newtime_stamp)
       os.utime(filename,(newtime_stamp,newtime_stamp))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文件系統(tǒng)模塊是一個(gè)封裝了標(biāo)準(zhǔn)的 POSIX 文件 I/O 操作的集合。通過(guò)require('fs')使用這個(gè)模塊。...
    保川閱讀 939評(píng)論 0 0
  • 寫(xiě)這篇小文章的緣由是之前做過(guò)一個(gè)項(xiàng)目,需要定期將文件轉(zhuǎn)走,也許專(zhuān)業(yè)點(diǎn)的名詞叫rollover。那么這就需要判斷一個(gè)...
    __七把刀__閱讀 2,187評(píng)論 0 1
  • 一、查看文件時(shí)間及相關(guān)命令 1、stat查看文件時(shí)間 [root@web10 ~]# stat install.l...
    虛心的鋤頭閱讀 8,084評(píng)論 0 0
  • 一.OS模塊 一).OS模塊的概念: Python os模塊是Python提供的訪問(wèn)操作系統(tǒng)功能的模塊,如打開(kāi)、讀...
    月亮是我踢彎得閱讀 4,109評(píng)論 0 3
  • 目錄與路徑絕對(duì)路徑與相對(duì)路徑:略目錄的相關(guān)操作幾個(gè)特殊目錄. 代表此層目錄.. 代表上一層目錄- 代表前一個(gè)工作目...
    March_13th閱讀 580評(píng)論 1 1

友情鏈接更多精彩內(nèi)容