I want to upload video on youtube from my iphone app..
I have tried this http://urinieto.com/2010/10/upload-videos-to-youtube-with-iphone-custom-app/
but in the end i am getting error
Domain=com.google.GDataServiceDomain Code=400 "The operation couldn’t be completed. (com.google.GDataServiceDomain error 400.)" UserInfo=0x4d921f0 {}
Can any one help me.
Thanx!!!
My code
NSString *devKey = [mDeveloperKeyField text];
GDataServiceGoogleYouTube *service = [self youTubeService];
[service setYouTubeDeveloperKey:devKey];
NSString *username = [mUsernameField text];
NSString *clientID = [mClientIDField text];
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username
clientID:clientID];
// load the file data
NSString *path = [[NSBundle mainBundle] pathForResource:#"YouTubeTest" ofType:#"m4v"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *filename = [path lastPathComponent];
// gather all the metadata needed for the mediaGroup
NSString *titleStr = [mTitleField text];
GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];
NSString *categoryStr = [mCategoryField text];
GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
[category setScheme:kGDataSchemeYouTubeCategory];
NSString *descStr = [mDescriptionField text];
GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];
NSString *keywordsStr = [mKeywordsField text];
GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];
BOOL isPrivate = mIsPrivate;
GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
[mediaGroup setMediaTitle:title];
[mediaGroup setMediaDescription:desc];
[mediaGroup addMediaCategory:category];
[mediaGroup setMediaKeywords:keywords];
[mediaGroup setIsPrivate:isPrivate];
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:path
defaultMIMEType:#"video/mp4"];
// create the upload entry with the mediaGroup and the file data
GDataEntryYouTubeUpload *entry;
entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
data:data
MIMEType:mimeType
slug:filename];
SEL progressSel = #selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
[service setServiceUploadProgressSelector:progressSel];
GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:entry
forFeedURL:url
delegate:self
didFinishSelector:#selector(uploadTicket:finishedWithEntry:error:)];
[self setUploadTicket:ticket];
First things first, check the YouTube API for any updates to it's GData libraries, (they changed last I used them and broke my app).
http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
Next, check for silly stuff. Got all your headers imported, everything checks out, etc?
Try setting your info.plist to use Wi-Fi, I've heard some of the new GData API's look for that might explain your 400 error, your app might not be pinging the YouTube servers.
Related
I have developed an app that loads a user's videos and when they click on the cell in the tableview it goes to another viewcontroller that holds a bunch of different information.
I'm trying to extract the actual YouTube link from a specific video. I print out the links I get and they come in this form.
https://www.youtube.com/v/Xf5pXlZJr1U?version=3&f=user_uploads&app=youtube_gdata
But I want it to be in this form.
http://www.youtube.com/watch?v=Xf5pXlZJr1U
Reason being that I want people to share the link via social media, but it comes out weird when it's in the first format.
Is there any way to easily convert the links?
SAMPLE CODE AND ANSWER EDIT
GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry2 title] stringValue];
NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:#"application/x-shockwave-flash" forKeyPath:#"type"];
NSString *tempURL = [flashContent URLString];
NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaThumbnails];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
UIImage *image = [UIImage imageWithData:data];
GDataYouTubeMediaGroup *mediaGroup = [(GDataEntryYouTubeVideo *)entry2 mediaGroup];
GDataMediaDescription *desc2 = [mediaGroup mediaDescription];
NSString *mystring = [desc2 stringValue];
NSArray *listItems = [tempURL componentsSeparatedByString:#"/"];
NSString *NewURL = #"";
NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:0]];
NewURL = [NewURL stringByAppendingString:#"//"];
NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:2]];
NewURL = [NewURL stringByAppendingString:#"/"];
NewURL = [NewURL stringByAppendingString:#"watch"];
NewURL = [NewURL stringByAppendingString:#"?v="];
NSArray *listItems2 = [[listItems objectAtIndex:4] componentsSeparatedByString:#"?"];
NewURL = [NewURL stringByAppendingString:[listItems2 objectAtIndex:0]];
If you take a look at NSString docs then you will find number of useful methods for yourself to use, e.g componentsSeparatedByString splitting the string in two different chunks depending on some particular character & stringByAppendingString for appending string to an existing string. It is not difficult to use these methods, just use according to your needs.
i am Parsing HTML Tag into iOS using Hpple. i am able to parse the data where the HTML Tag is
<div id="NewsPageSubTitle">
<p><**span** hi how are you>
Using ios code:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p/span ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but in few case i don't have span, imean the string in html is accessed by tag "p" directly like:
<div id="NewsPageSubTitle">
<p>< hi how are you>
Here I am using ios code as:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but here i am getting a blank data in response.
can any one let me know how to solve the problem?
Since sometimes the para tag has span and sometimes it does not, I would suggest trying to handle that by looping over the children
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
TFHpple * tutorialsParser = [[TFHpple alloc] initWithHTMLData:data];
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageSubTitle']";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
for (TFHppleElement * element in tutorialsNodes) {
NSLog(#"%#", element);
NSLog(#"%#", [element tagName]);
NSLog(#"%#", [element attributes]);
NSLog(#"%#", [element children]);
for (TFHppleElement *childElement in [element children]) {
NSLog(#"%#", childElement);
}
}
Check with this: https://github.com/mwaterfall/MWFeedParser
This will provide the HTML Parser for iphone sdk.
More help on:
this blog and here.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"html" inDirectory:#"New Folder 2"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSString *htmlString = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
TFHpple * Parser = [[TFHpple alloc] initWithHTMLData:data];
NSString *query = #"//p";
NSArray *nodes = [Parser searchWithXPathQuery:query];
for (TFHppleElement *item in nodes)
{
NSLog(#"Title : %#", item.content);
NSLog(#"URL : %#", [item.attributes valueForKey:#"href"]);
}
I'm trying to post video to youtube from inside of my iPhone application using the GData API. Here is the code I'm using:
GDataServiceGoogleYouTube* service = [self youTubeService];
[service setYouTubeDeveloperKey:youtubeAppKey];
NSString *username = service.username;
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username];
NSData *data = [NSData dataWithContentsOfFile:self.videoPath];
NSString *filename = #"My Cool Video";
NSString *titleStr = #"Title";
GDataMediaTitle *mediaTitle = [GDataMediaTitle textConstructWithString:titleStr];
NSString *categoryStr = #"Comedy";
GDataMediaCategory *mediaCategory = [GDataMediaCategory mediaCategoryWithString:categoryStr];
[mediaCategory setScheme:kGDataSchemeYouTubeCategory];
NSString *descStr = #"Description";
GDataMediaDescription *mediaDesc = [GDataMediaDescription textConstructWithString:descStr];
NSString *keywordsStr = #"iOS";
GDataMediaKeywords *mediaKeywords = [GDataMediaKeywords keywordsWithString:keywordsStr];
GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
[mediaGroup setMediaTitle:mediaTitle];
[mediaGroup setMediaDescription:mediaDesc];
[mediaGroup addMediaCategory:mediaCategory];
[mediaGroup setMediaKeywords:mediaKeywords];
[mediaGroup setIsPrivate:isPrivate];
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:self.videoPath
defaultMIMEType:#"video/mp4"];
GDataEntryYouTubeUpload *entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
data:data
MIMEType:mimeType
slug:filename];
GDataServiceTicket *ticket = [service fetchEntryByInsertingEntry:entry
forFeedURL:url
delegate:self
didFinishSelector:#selector(uploadTicket:finishedWithEntry:error:)];
[self setUploadTicket:ticket];
And I'm having this response:
serviceBase: objectFetcher:GTMHTTPUploadFetcher 0x48af0a0 (https://uploads.gdata.youtube.com/resumable/feeds/api/users/myCoolMail#gmail.com/uploads?upload_id=heregoesidiremoved) failedWithStatus:400 data:GDataInvalidRequestUriExceptionException message unavailable
I've tried Google sample code and had the same result. I used device and Simulator (5.0). I've tried prepared video as long as the recorded from iPhone one.
I created the Project in the google API section and got an application key.
Please, help me find the solution of the problem!
The problem was here:
NSString *username = service.username;
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username];
Don't know why is that, but I've changed the userID from "username" to kGDataServiceDefaultUser and the thing started working. So simple and so odd, but it did the trick.
Correct code:
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:kGDataServiceDefaultUser];
I am using following code to upload .mov file to youtube. It upload successfully but the video don't have any audio. Please tell me if I am doing something wrong....
- (void)uploadVideoFile {
NSString *devKey = #"AI39si4w9QSgQj1JzNxzuiHhFbTm2iLt3mRerGh0UNvlryRPGgQhgIaJA8l95j4YwdC1jBIz6_JaX8eJm2GMgE06FtvIu7E9Sg";
GDataServiceGoogleYouTube *service = [self youTubeService];
[service setYouTubeDeveloperKey:devKey];
NSString *username = txtYName.text;
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username];
// load the file data
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"Documents/movie.mov"]] retain];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *filename = [path lastPathComponent];
// gather all the metadata needed for the mediaGroup
NSString *titleStr = txtName.text;
GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];
NSString *categoryStr = #"Comedy";
GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
[category setScheme:kGDataSchemeYouTubeCategory];
NSString *descStr = #"this is just test";
GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];
NSString *keywordsStr = #"SuperShredBros";
GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];
BOOL isPrivate = NO;
GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
[mediaGroup setMediaTitle:title];
[mediaGroup setMediaDescription:desc];
[mediaGroup addMediaCategory:category];
[mediaGroup setMediaKeywords:keywords];
[mediaGroup setIsPrivate:isPrivate];
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:path
defaultMIMEType:#"video/quicktime"];
// create the upload entry with the mediaGroup and the file data
GDataEntryYouTubeUpload *entry;
entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
data:data
MIMEType:mimeType
slug:filename];
SEL progressSel = #selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
[service setServiceUploadProgressSelector:progressSel];
GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:entry
forFeedURL:url
delegate:self
didFinishSelector:#selector(uploadTicket:finishedWithEntry:error:)];
[self setUploadTicket:ticket];
// [self updateUI];
}
please help me. Thanks in advance.
I think you need to change mime type as below
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:fetchPath
defaultMIMEType:#"video/mp4"];
This thing works fine for me and also one more thing i am uploading .Mov file to not only .mp4 or .m4a or .flv
Following should help you:
GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
[mediaGroup setMediaTitle:title];
[mediaGroup setMediaDescription:desc];
[mediaGroup addMediaCategory:category];
[mediaGroup setMediaKeywords:keywords];
[mediaGroup setIsPrivate:NO];
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:outputURL.relativePath defaultMIMEType:#"video/quicktime"];
GDataEntryYouTubeUpload *entry;
entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup data:data MIMEType:mimeType slug:filename];
SEL progressSel = #selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
[service setServiceUploadProgressSelector:progressSel];
GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:entry forFeedURL:url delegate:self didFinishSelector:#selector(uploadTicket:finishedWithEntry:error:)];
for reference: Youtube Upload Quality
change your output audio setting.... it should be like this.....
audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
[ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
[ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
[ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
nil];
i think Youtube supports flv and mp4 format video files. so please change mimetype.
Hope you get expected results.
i got the following problem :
i am trying to create a file with the following Code :
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle FileHandleForWritingAtPath:filePath]retain];
NSString *data = #"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];
My Question is the following. Running this on a MAC does not give me any Errors whatsoever. But the File itself seems to be nonexistant or at least not locateable.
I need to find and access the File and i have to be able to export it to my Mac using a mobile iOS Device
thanks in advance :)
h4wkeye
You need to check that savedFile is not nil (see the docs here).
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle fileHandleForWritingAtPath:filePath]retain];
if (nil == savedFile) {
NSLog(#"Could not create saved file at %#", filePath);
}
NSString *data = #"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];