admin管理员组

文章数量:1287655

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.

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.

Share Improve this question edited Feb 24 at 3:08 HangarRash 15.1k5 gold badges19 silver badges55 bronze badges asked Feb 24 at 2:40 VillahousutVillahousut 1451 gold badge1 silver badge14 bronze badges 6
  • 1 What do you get with creationDate.load(.stringValue) ? – HangarRash Commented Feb 24 at 3:14
  • 1 Note, you cannot get the timezone from a Date that you get in your dateValue. A Date is "A specific point in time, independent of any calendar or time zone." In other words always UTC timezone. – workingdog support Ukraine Commented Feb 24 at 3:30
  • 1 Note that Timezones do sometimes change depending on the year it is setup or changed, or according to seasons, eg summer winter. – workingdog support Ukraine Commented Feb 24 at 3:52
  • @HangarRash I get a nil. I understand the Date itself doesn't have a timezone. I'm asking where exactly the timezone is stored then, and how I use AVFoundation to access it. – Villahousut Commented Feb 25 at 4:02
  • That's strange that you get nil for the string value. According to the documentation for creationDate, the date value is derived from the string value. I thought maybe the original string value would contain the original timezone in it. – HangarRash Commented Feb 25 at 4:05
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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/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: monIdentifierCreationDate)
         print("---> creationDates: \(creationDates)\n")
         
         for item in meta {
             if let key = itemmonKey?.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.

本文标签: swiftIn AVFoundationhow do I find out the timezone of the creationDateStack Overflow