3

I want to play a video , which format is video.h264

I have tried with AVPlayer and MPMoviePlayerViewController

But, I failed to play. Is there an way to do this. Any other library available for achieve the same

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • 3
    People are beginning to over-use the `vote-to-close` system. This is a perfectly valid question; correct me if I'm wrong. – Albert Renshaw Feb 21 '17 at 09:15

3 Answers3

0

First, read this post. Possible Locations for Sequence/Picture Parameter Set(s) for H.264 Stream

A .h264 file is in annex b format. Next read this:

https://developer.apple.com/reference/videotoolbox/vtdecompressionsession

Convert from annex b to avcc, set extradata when opening the decompression session. Decode frames -> display frames.

Community
  • 1
  • 1
szatmary
  • 29,969
  • 8
  • 44
  • 57
  • Very nice linked answer in the first link! The second link is interesting too - this looks like Beta functionality on iOS at the moment - is this right? Given the complexity of video encoding and packaging it is useful sometimes to leverage existing libraries. ffmpeg will support packaging a h.264 file into an mp4 container which I think iOS players find more palatable - is this true? In Android reliable and well used fmmpeg wrapper exist, if the conversions must be done on the mobile (easier to do it server side, of course) and I think they are available in iOS also. – Mick Feb 22 '17 at 12:51
  • No it's not beta, it's been in iOS since version 8. And wasn't beta then. If your going to wrap the es in mp4, do it server side, not in the client. Your question was how to play an h264 file, Not how to convert it to another format. – szatmary Feb 22 '17 at 14:28
  • Ok - the Beta question was because the linked apple developer page above has a note at the bottom which seems to imply it might be Beta, Maybe just the way they have the page set up. Not my question, btw :) Agree about the server side comment. – Mick Feb 22 '17 at 14:56
-2

Here is some code that does just that:

- (void)viewDidLoad {
  [super viewDidLoad];
  UIView *view = self.view;
  NSString *resourceName = @"SunSpot_1080p_main.m4v";
  NSString* movieFilePath = [[NSBundle mainBundle]
    pathForResource:resourceName ofType:nil];
  NSAssert(movieFilePath, @"movieFilePath is nil");
  NSURL *fileURL = [NSURL fileURLWithPath:movieFilePath];

  AVPlayerViewController *playerViewController =
    [[AVPlayerViewController alloc] init];
  playerViewController.player =
    [AVPlayer playerWithURL:fileURL];
  self.avPlayerViewcontroller = playerViewController;
  [self resizePlayerToViewSize];
  [view addSubview:playerViewController.view];
  view.autoresizesSubviews = TRUE;
}

The full source code and github link can be found at my blog post on the subject. This new player view controller is very easy as compared to previous APIs, there really is nothing to it.

MoDJ
  • 4,309
  • 2
  • 30
  • 65
-2

Unless something has changed recently (which is always possible in the video world...) iOS does not support 'raw' h264 video playback - e.g. MyVideoFile.h264.

Its worth mentioning that a video is typically wrapped like this when it gets to an iOS device, if it is being streamed:

  • raw pixels bitmap
  • raw pixels encoded (e.g. h.264 encoded)
  • encoded video stream packaged into container with audio streams, subtitles etc (e.g. mp4 container)
  • container broken into 'chunks' or segments for streaming (on iOS using HLS streaming format)

If your video is short you may just have to package it into an mp4 container and it should play back then - i.e. create MyVideoFile.mp4 from your original file.

If it is longer, and you are streaming it, you will generally have to also have to use the HLS streaming format as this is one of Appel's rules (see https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/UsingHTTPLiveStreaming/UsingHTTPLiveStreaming.html#//apple_ref/doc/uid/TP40008332-CH102-SW5):

If your app delivers video over cellular networks, and the video exceeds either 10 minutes duration or 5 MB of data in a five minute period, you are required to use HTTP Live Streaming. (Progressive download may be used for smaller clips.)

Mick
  • 24,231
  • 1
  • 54
  • 120