Home > Blockchain >  How to get video format from video URL (Any type, not just AVPlayer compatible)
How to get video format from video URL (Any type, not just AVPlayer compatible)

Time:08-20

I want to get the format from a video URL (mp4, m3u8, mpeg etc...) to decided which player to use in my app (AVPlayer or third party).

One option is to check if AVAsset(...).isPlayable, Im using this right now, but I want to improve my logic for when the AVPlayer can't be used.

When I can't use AVPlayer then I encode the video using ffmpeg, if I could have the format of the video (and maybe even more information) I could improve the encode done by ffmpeg.

CodePudding user response:

I would use a Regex expression to analyse the URL and find the extension.

you can use this one : .\w{3,4}($|?)

CodePudding user response:

If you do NOT want to use Regex, then this code should do the trick :

var url = "https://www.bruno.com/videos/video.mp4"

extension String {

    func fileExtension() -> String {
        return URL(fileURLWithPath: self).pathExtension
    }
}

let fileExtension = url.fileExtension()

print(fileExtension)

CodePudding user response:

You can use something like below

let url = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")

let result = url?.pathExtension
    //result will return the extension of the given path

Hope this helps you

  • Related