How to convert mp4 to NSData?
Asked Answered
P

2

0

enter image description hereI have file in XCode with name vvideo.mp4. I am trying to convert it to NSData like below

    NSData *data = [NSData dataWithContentsOfFile:@"vvideo.mp4" options:nil error:nil];

But as I log data, it returns null.

What is right way to convert any video to NSData?

File is added and copies properly in Xcode, you can see here.

EDITED QUESTION....

Poleaxe answered 24/10, 2013 at 12:0 Comment(3)
That's the right way. Your file must just not be named like this, or you haven't included it in your project. I mean, this creates a NSData with the contents of the file, loaded in memory. So that's also the right way to bring your device to its knees by loading a whole movie in RAM.San
edited the Question, check itPoleaxe
Its of few seconds, only 10 seconds video sir....Poleaxe
S
8

dataWithContentsOfFile:options:error: takes a file path and not file name. If the video file is in application bundle you should do,

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"vvideo" ofType:@"mp4"];

NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:filePath options:nil error:&error];
if(data == nil && error!=nil) {
    //Print error description
}

Rather than passing error argument as nil pass the value, it will be useful in understanding error condition, if any occurs.

Hope that helps!

Samford answered 24/10, 2013 at 12:6 Comment(12)
2013-10-24 17:09:56.793 GascoLiveStream[17380:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData initWithContentsOfFile:options:error:]: nil file argument'Poleaxe
@Poleaxe Print filePath and check if it correct, pass error argument and check what is the error.Samford
options:nil here is the issue and warning daerPoleaxe
@Poleaxe What is the warning? You can pass it as NSDataReadingUncached.Samford
@Poleaxe Check my edit, pass NSError argument. Print it and check what is the error. Let me know as well.Samford
-[NSConcreteData initWithContentsOfFile:options:error:]: nil file argument'Poleaxe
It does not go inside If body, as it crashes there.Poleaxe
@Poleaxe What is the file path? Can you tell me?Samford
I simply add this to Supported Files.Poleaxe
dear, as I use dataWithContentsOfURL and pass URL, it works, but locally it is not converting any videoPoleaxe
@Poleaxe You have still not told me what I asked you. Did you print filePath? What does it show? nil file argument means the file does not exist at the location you are reading it from.Samford
Dear sorry for late @Amar, its null. Now I will try to correct it.Poleaxe
H
0

This is how you can do this in Swift 5:

if let path = Bundle.main.path(forResource: "download", ofType: "mp4"),
   let data = try? Data.init(contentsOf: URL(fileURLWithPath: path)) {
      // Use this data
}

Very important thing to note here is that the file has to be somewhere inside the project instead of Assets.xcassets otherwise path would be nil.

Also make sure to associate the correct Target Membership for example:

enter image description here

enter image description here

Hereford answered 6/6, 2022 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.