Villahousut
Villahousut

Reputation: 145

In AVFoundation, how do I find out the timezone of the creationDate?

My program needs to read video metadata to find out the creation date in local time for the video when and where it was recorded. How do I do that?

I've gotten as far as:

// Create an AVAsset for the video file
let asset = AVURLAsset(url: fileURL)
print("Loaded AVURLAsset \(String(describing: asset))")

if let creationDate = try await asset.load(.creationDate) {
    if let dateValue = try await creationDate.load(.dateValue) {
        print("Creation date from metadata: \(dateValue)")
    } else {
        print("No parsable creation ate from metadata")
    }
} else {
    print("No creation ate from metadata")
}

However, the creationDate is in UTC. If I compare to the file creation date:

Creation date from metadata: 2025-02-23 19:14:20 +0000
Creation date for file: 2025-02-23 11:14:28 -0800

How do I find out that specific video's local time or timezone when it was recorded?

Please note that I don't want to convert the UTC time to local time based on the timezone of the computer running the program, I want to use the specific local time or timezone of the video in question. For instance, the video could have been recorded in a different timezone compared to where my computer happens to be in right now.

Upvotes: 1

Views: 64

Answers (1)

If the timezone has been added to the video asset meta data (eg using AVMutableMetadataItem), then you could try using this code to find it:

 func test() async {
     let fileURL = URL(string: "https://sample-videos.com/video321/mp4/720/big_buck_bunny_720p_1mb.mp4")!
     
     let asset = AVURLAsset(url: fileURL)
     print("---> asset \(asset)\n")
     
     do {
         let meta = try await asset.load(.metadata)
         print("---> metadata: \(meta)\n")
         
         let creationDates = AVMetadataItem.metadataItems(
             from: meta,
             filteredByIdentifier: .commonIdentifierCreationDate)
         print("---> creationDates: \(creationDates)\n")
         
         for item in meta {
             if let key = item.commonKey?.rawValue {
                 let value = try await item.load(.value)
                 print("---> key: \(key)  value: \(value)")
             } else {
                 print("Unknown")
             }
         }
     } catch {
         print(error)
     }
 }
 

Note, with the given example, this did not give any dates or timezone.

Upvotes: 0

Related Questions