0

I am loading an video from a URL provided by a third-party. There is no file extension (or filename for that matter) on the URL (as it is an obscured URL). I can take the data from this (in the form of NSData) and load it into a video player and display it fine.

I want to persist this data to a file. However, I don't know what format the data is in (mp4, wav)? I assume it is mp4 (since it's an video from the web) but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything. I just wanted to know the file extension whether it is an image or video.

Developer007
  • 437
  • 3
  • 12
  • In addition to checking the content type, virtually all photo, audio, and video file formats begin (in the first 8-16 bytes) with some sort of "signature" that can be checked, either with ad-hoc code (examine a few files and figure it out) or with a canned routine you can probably find on the web (if one isn't built-in to iOS). – Hot Licks Nov 06 '14 at 12:45
  • Are you check this links.... http://stackoverflow.com/questions/26751051/finding-file-type-of-nsdata-recieved-from-server?rq=1 http://stackoverflow.com/questions/10713419/play-nsdata-into-audio-format-ios?rq=1 – Naveen kumar Nov 07 '14 at 05:40
  • I have checked these links. But i want the format of NSData downloaded or extension of file. It may be either audio video or image. For image i am able to check the format or extension. But for video and audio i am not getting proper method to check. – Developer007 Nov 07 '14 at 05:59

1 Answers1

1

You should check the content type returned by the server:

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)theResponse
  {
    NSString* content_type = [[(NSHTTPURLResponse*)theResponse allHeaderFields] valueForKey:@"Content-Type"];

    //content_type might be image/jpeg, video/mp4 etc.
  }
user623396
  • 1,537
  • 1
  • 17
  • 39