Last active
November 7, 2020 14:47
-
-
Save SheffieldKevin/6e9de245bc214d9be7c6 to your computer and use it in GitHub Desktop.
Playing with movie tracks using AVFoundation in Swift (command line version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env swift | |
import Foundation | |
import AVFoundation | |
if Process.arguments.count != 2 { | |
println("The first and only argument should be a full path to a movie or music file.") | |
exit(1) | |
} | |
if let url = NSURL(fileURLWithPath:Process.arguments[1]) { | |
let theOpts = [ | |
AVURLAssetPreferPreciseDurationAndTimingKey : true, | |
AVURLAssetReferenceRestrictionsKey : 0 // AVAssetReferenceRestrictions.RestrictionForbidNone | |
] | |
if let movie = AVURLAsset(URL:url, options:theOpts) { | |
println("We have a movie") | |
println("==============================") | |
println("Getting the total number of tracks:") | |
println("Movie has \(movie.tracks.count) tracks") | |
println("==============================") | |
println("Getting the number of tracks based on their characteristics") | |
println("Movie has \(movie.tracksWithMediaCharacteristic(AVMediaCharacteristicVisual).count) visual tracks") | |
println("Movie has \(movie.tracksWithMediaCharacteristic(AVMediaCharacteristicAudible).count) audible tracks") | |
println("==============================") | |
println("Getting the number of tracks based on their mediaType") | |
println("Movie has \(movie.tracksWithMediaType(AVMediaTypeVideo).count) video tracks") | |
println("Movie has \(movie.tracksWithMediaType(AVMediaTypeAudio).count) audio tracks") | |
println("==============================") | |
println("Now getting the CMPersistentTrackID for each track") | |
var trackID:CMPersistentTrackID = CMPersistentTrackID(kCMPersistentTrackID_Invalid) | |
for (index, track) in enumerate(movie.tracks) { | |
trackID = track.trackID | |
println("Track at index: \(index) has track ID: \(trackID)") | |
} | |
println("==============================") | |
println("Now get the track with the last track ID") | |
let track:AVAssetTrack = movie.trackWithTrackID(trackID) | |
println("Track with trackID: \(trackID) is: \(track)") | |
println() | |
println("==============================") | |
println("Lets get the track segments of one of the last track") | |
for (index, segment) in enumerate(track.segments) { | |
let sourceRange = CMTimeRangeCopyAsDictionary(segment.timeMapping.source, kCFAllocatorDefault) | |
println("Segment \(index) time range for source: \(sourceRange)") | |
let targetRange = CMTimeRangeCopyAsDictionary(segment.timeMapping.target, kCFAllocatorDefault) | |
println("Segment \(index) time range for target: \(targetRange)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment