How to get video duration fro AVAsset in Swift
You shouldn't cast your url to NSURL. Just get your documents directory url, and append your filename and extension to it. Besides that you can get CMTime
seconds property which is a double and use String(format:)
method to properly display the time in "h m s"
format:
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentDirectory.appendingPathComponent(downs[i])
let duration = AVURLAsset(url: videoURL).duration.seconds
print(duration)
let time: String
if duration > 3600 {
time = String(format:"%dh %dm %ds",
Int(duration/3600),
Int((duration/60).truncatingRemainder(dividingBy: 60)),
Int(duration.truncatingRemainder(dividingBy: 60)))
} else {
time = String(format:"%dm %ds",
Int((duration/60).truncatingRemainder(dividingBy: 60)),
Int(duration.truncatingRemainder(dividingBy: 60)))
}
print(time)
Get the accurate duration of a video
This works for me:
import AVFoundation
import CoreMedia
...
if let url = Bundle.main.url(forResource: "small", withExtension: "mp4") {
let asset = AVAsset(url: url)
let duration = asset.duration
let durationTime = CMTimeGetSeconds(duration)
print(durationTime)
}
For the video here it prints "5.568" which is correct.
Edit from comments:
A video that returns 707
seconds when divided by 60 sec/min
is 11.78
. This is 11.78 minutes, or 11 minutes and 0.78min * 60sec/min = 47sec
, total is 11 min 47 sec
iOS: get video duration and thumbnails without playing video
To get the duration:
NSURL *sourceMovieURL = [NSURL fileURLWithPath:somePath];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
CMTime duration = sourceAsset.duration;
To get a framegrab:
AVAssetImageGenerator* generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:destinationAsset];
//Get the 1st frame 3 seconds in
int frameTimeStart = 3;
int frameLocation = 1;
//Snatch a frame
CGImageRef frameRef = [generator copyCGImageAtTime:CMTimeMake(frameTimeStart,frameLocation) actualTime:nil error:nil];
How to get video full duration and current playing time?
You can use CMTimeGetSeconds to converts a CMTime to seconds.
let durationTime = CMTimeGetSeconds(duration)
AVAsset video adjust duration
Based on my reading of the docs, it looks like that self.asset.duration.timescale
may be the key here, as changing it will influence the whole file (if I'm understanding the reference you're making that that timescale is for the whole file, or maybe you need to adjust it in each of the buffers).
See here for more info as well.
Relevant section:
A CMTime is represented as a rational number, with a numerator (an
int64_t value), and a denominator (an int32_t timescale).
Conceptually, the timescale specifies the fraction of a second each
unit in the numerator occupies. Thus if the timescale is 4, each unit
represents a quarter of a second; if the timescale is 10, each unit
represents a tenth of a second, and so on. In addition to a simple
time value, a CMTime can represent non-numeric values: +infinity,
-infinity, and indefinite. Using a flag CMTime indicates whether the time been rounded at some point.CMTimes contain an epoch number, which is usually set to 0, but can be
used to distinguish unrelated timelines: for example, it could be
incremented each time through a presentation loop, to differentiate
between time N in loop 0 from time N in loop 1
iOS- how to get the duration of .mp4 file by using AVAsset or AVURLAsset
Yes, I've had similar problems before. For me the problem went away when I used:
NSURL *itemPathURL = [NSURL fileURLWithPath:itemPathString];
instead of
NSURL *itemPathURL = [NSURL URLWithString:itemPathString];
If you're completely sure your path is ok, this should fix your problem.
Hope this helps!
Related Topics
Update Text in While Loop Swift
Sending Variables with a Segue
How to Decode Utf8-Literals Like "\Xc3\Xa6" in Swift 5
Get Images from Document Directory Not File Path Swift 3
No Such Module Crashlytics - Pod Seems to Be Missing
Failable Initializers with Codable
Where to Implement Nsvaluetransformer for Core Data in Swift
Get All Documents at Once in a Completion Handler with Getdocuments in Firestore
How to Deallocate All References Elements from an Array
Iterate a Grid of Views Swiftui
Firebase Datadescription Returns Empty Array
How to Make a Uiview Grow in Uiview.Animate() with an Accurate Corner Radius
Getting Wrong Date from Dateformat