17

Using AVPlayer in a Mac app here to play random videos in fullscreen from a folder, but when I try to play .vob files or .mpg files I just get a black screen that lasts as long as the video lasts.

Does AVFoundation not support playback from these containers? I figured that since they were playable with stock QuickTime Player they would also work with AVPlayer.

Christian A. Strømmen
  • 3,181
  • 2
  • 25
  • 46

3 Answers3

28

The AVURLAsset class has a static methods that you can query for supported video UTIs:

+ (NSArray *)audiovisualTypes

On 10.9.1 it returns these system defined UTIs:

  • public.mpeg
  • public.mpeg-2-video
  • public.avi
  • public.aifc-audio
  • public.aac-audio
  • public.mpeg-4
  • public.au-audio
  • public.aiff-audio
  • public.mp2
  • public.3gpp2
  • public.ac3-audio
  • public.mp3
  • public.mpeg-2-transport-stream
  • public.3gpp
  • public.mpeg-4-audio

Here is an explanation of system UTIs. So it seems that at least the .mpg container should be supported.

According to wiki, .mpg files can contain MPEG-1 or MPEG-2 video, but only MPEG-2 video is supported. So maybe that's why the file loads but nothing is displayed.

QuickTime internally uses QTMovieModernizer in order to play videos in legacy formats (as mentioned in this WWDC session), so maybe you can look into that. It even has a method for determining if a file needs to be modernized:

+ requiresModernization:error:
Janis Kirsteins
  • 2,128
  • 16
  • 17
  • I just did a requiresModernization:error: on the files here, and I get NO back. – Christian A. Strømmen Feb 24 '14 at 14:06
  • 1
    Checked the mpg file here and it is MPEG-1 for the video. So how would I convert this since QTMovieModernizer refuses to? It returns no for that class method for both the mpg and the vob, and when I try to still modernise the files it completes with status QTMovieModernizerStatusNotRequired. – Christian A. Strømmen Feb 24 '14 at 14:47
  • Not sure, in that case. I figured it must be some legacy format at fault. Maybe you can use something like FFmpeg to check the videos for errors or get more information about it (e.g. see what 'ffmpeg -i myfile.avi' outputs). Then, if it reports errors, or, if it reports that the file is in some unexpected video format, you can integrate those checks and/or transcoding into your app using FFmpeg libraries. – Janis Kirsteins Feb 25 '14 at 08:34
3

To get a list of supported extensions, you can use the following function:

import AVKit
import MobileCoreServices

func getAllowedAVPlayerFileExtensions() -> [String] {
    let avTypes = AVURLAsset.audiovisualTypes()
    var avExtensions = avTypes.map({ UTTypeCopyPreferredTagWithClass($0 as CFString, kUTTagClassFilenameExtension)?.takeRetainedValue() as String? ?? "" })
    avExtensions = avExtensions.filter { !$0.isEmpty }
    return avExtensions
}

This will return a list like so:

["caf", "ttml", "au", "ts", "mqv", "pls", "flac", "dv", "amr", "mp1", "mp3", "ac3", "loas", "3gp", "aifc", "m2v", "m2t", "m4b", "m2a", "m4r", "aa", "webvtt", "aiff", "m4a", "scc", "mp4", "m4p", "mp2", "eac3", "mpa", "vob", "scc", "aax", "mpg", "wav", "mov", "itt", "xhe", "m3u", "mts", "mod", "vtt", "m4v", "3g2", "sc2", "aac", "mp4", "vtt", "m1a", "mp2", "avi"]
DZoki019
  • 382
  • 2
  • 13
ThePedestrian
  • 819
  • 2
  • 11
  • 22
  • While not exactly the requirement of this question and since such questions get so little views, I thought I'd ask here - is it possible to know the current media file format being played by the AVPlayer if you are streaming live data ? – Shawn Frank Jun 16 '21 at 13:13
1

Swift 5, iOS 15+

import AVFoundation

func getAllowedAVPlayerFileExtensions() -> [String] {
    let avTypes: [AVFileType] = AVURLAsset.audiovisualTypes()
    
    let avExtensions: [String] =
        avTypes
            .compactMap({ UTType($0.rawValue)?.preferredFilenameExtension })
            .sorted()

    return avExtensions
}

Returns sorted file extensions list:

["3g2", "3gp", "aa", "aac", "aax", "ac3", "aifc", "aiff", "amr", "au", "avi", "caf", "eac3", "flac", "itt", "loas", "m1a", "m2a", "m3u", "m4a", "m4b", "m4p", "m4r", "m4v", "mov", "mp1", "mp2", "mp3", "mp4", "mp4", "mpa", "mqv", "pls", "ttml", "vtt", "vtt", "w64", "wav", "webvtt", "xhe"]
Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38