ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python/Mac/POSIX/SetFile로 파일 생성일 변경
    코딩/Python 2024. 1. 23. 16:09
    728x90

    GoPro Max 등의 360 카메라는 영상을 촬영하고 후처리를 통해 MP4 영상을 추출한다. 이렇게 만들어진 MP4 동영상은 촬영일자가 실제 영상을 촬영한 날짜와 달라 파일관리와 영상편집등에서 사용하기 불편하다.

    그래서 영상 촬영일과 시간으로 파일이름을 만들고 파이썬으로 파일이름에 따라 파일생성일을 변경해준다.

    os.stat

    Windows에서는 os.utime()으로 날짜를 변경하는데 문제가 없는데 POSIX(Mac OS)에서는 파일의 액세스, 내용수정시간은 변경할 수 있어도 생성일을 변경할 수는 없다.

    os.stat_result의 타임스탬프는 아래와 같다.

    • st_atime: 초 단위의 가장 최근의 액세스 시간.
    • st_mtime: 초 단위의 가장 최근의 내용 수정 시간.
    • st_ctime: 플랫폼에 따라 유닉스에서 가장 최근의 메타 데이터 변경 시간, 윈도우에서 생성 시간, 단위는 초.
    • st_birthtime: 파일 생성 시간. 조회만 할 수 있고 변경할 수 없다.(POSIX만, Windows는 가능)

     

    728x90

    Command line utility SetFile

    맥에서 생성일 수정은 setfile을 이용하면 가능하다. setfile은 Xcode Command Line Tools에 포함된 유틸리티 중 하나이기 때문에 이 도구를 사용하려면 해당 도구가 설치되어 있어야 한다. 자주 사용하는 기능은 아니기 때문에 가끔 에러가 나기도 하는데, 이것은 업데이트가 되어 있지 않기 때문이거나, 처음부터 에러가 난다면 Xcode Command Line Tools을 설치하지 않아서이다.

    Xcode Command Line Tools 설치:

    xcode-select --install

    Xcode 업데이트

    Xcode가 이미 설치되어 있고 최신 버전이 아닌 경우 App Store에서 Xcode를 업데이트할 수 있다.

    Xcode 선택

    터미널에서 다음 명령어를 실행하여 Xcode를 선택.

    sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

    Xcode의 설치 경로가 /Applications/Xcode.app가 아니라면 해당 경로를 명시적으로 지정해야 한다.

     

    위의 단계를 따라 했음에도 문제가 해결되지 않는다면, Xcode Command Line Tools가 올바르게 설치되지 않은 것일 수 있다. 이 경우에는 Xcode를 완전히 삭제하고 다시 설치한 후, xcode-select --install 명령어로 Command Line Tools를 설치하는 것이 좋다.


    또한 macOS 업데이트를 통해 시스템을 최신 상태로 유지하는 것도 중요하다.

    사용법

    SetFile의 사용법은 아래와 같다.

    Usage: SetFile [option...] file...
        -a attributes     # attributes (lowercase = 0, uppercase = 1)*
        -c creator        # file creator
        -d date           # creation date (mm/dd/[yy]yy [hh:mm[:ss] [AM | PM]])*
        -m date           # modification date (mm/dd/[yy]yy [hh:mm[:ss] [AM | PM]])*
        -P                # perform action on symlink instead of following it
        -t type           # file type
    
        Note: The following attributes may be used with the -a option:
            A   Alias file
            B   Bundle
            C   Custom icon*
            D   Desktop*
            E   Hidden extension*
            I   Inited*
            M   Shared (can run multiple times)
            N   No INIT resources
            L   Locked
            S   System (name locked)
            T   Stationery
            V   Invisible*
            Z   Busy*
    
        Note: Items marked with an asterisk (*) are allowed with folders
        Note: Period (.) represents the current date and time.
        Note: [yy]yy < 100 assumes 21st century, e.g. 20yy

    그리고 아래 코드로 파일명에 따라 생성일을 변경한다.

    from datetime import datetime
    import os
    from subprocess import call
    
    path = '/your/path'
    f = '221125-152456.MP4'
    fp = os.path.join(path, f)
    
    fname, _ = os.path.splitext(f)
    format_fname = '%y%m%d-%H%M%S'
    format_time = '%m/%d/%Y %H:%M:%S'
    t_filename = datetime.strptime(fname, format_fname)
    
    t_stamp = os.stat(fp).st_birthtime
    t_f = datetime.fromtimestamp(t_stamp)
    
    if not t_f == t_filename:
        time = datetime.strftime(t_filename, format_time)
        cmd = f'SetFile -d "{time}" -m "{time}" "{fp}"'
        call(cmd, shell=True)

    이미지 파일의 경우는 생성일을 변경하지 않고 exif 데이터를 수정해서 관리할 수 있다.

    728x90

    댓글

Designed by Tistory.