Commit 0361c54d authored by Pierre d'Herbemont's avatar Pierre d'Herbemont

MacOSX/Framework/Example: FlashVideoDownloader, sample app that download a...

MacOSX/Framework/Example: FlashVideoDownloader, sample app that download a video from YouTube, dailymotion, etc. using VLC's luaplaylist module, and export them to a more readable format. (killer app, imho :), but yet only an code Example. )
parent 7bdcff97
/* Controller */
#import <Cocoa/Cocoa.h>
#import <VLCKit/VLCKit.h>
@interface Controller : NSObject
{
IBOutlet NSView * remoteURLView;
IBOutlet NSView * workingView;
IBOutlet NSWindow * window;
IBOutlet NSButton * openConvertedFileButton;
NSNumber * selectedStreamOutput;
NSString * remoteURLAsString;
VLCMedia * media;
VLCStreamSession * streamSession;
NSString * outputFilePath;
NSString * outputFolderPath;
}
- (void)awakeFromNib;
@property (retain) VLCMedia * media;
@property (retain) VLCStreamSession * streamSession;
@property (assign) NSNumber * selectedStreamOutput;
@property (retain,readonly) NSString * outputFilePath;
@property (retain,readonly) NSString * outputFolderPath;
@property (retain) NSString * remoteURLAsString;
- (IBAction)convert:(id)sender;
- (IBAction)openConvertedFile:(id)sender;
- (IBAction)openConvertedEnclosingFolder:(id)sender;
- (IBAction)pickOutputFolderPath:(id)sender;
@end
#import "Controller.h"
/**********************************************************
* First off, some value transformer to easily play with
* bindings
*/
@interface VLCFloat10000FoldTransformer : NSObject
@end
@implementation VLCFloat10000FoldTransformer
+ (Class)transformedValueClass
{
return [NSNumber class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
if( !value ) return nil;
if(![value respondsToSelector: @selector(floatValue)])
{
[NSException raise: NSInternalInconsistencyException
format: @"Value (%@) does not respond to -floatValue.",
[value class]];
return nil;
}
return [NSNumber numberWithFloat: [value floatValue]*10000.];
}
- (id)reverseTransformedValue:(id)value
{
if( !value ) return nil;
if(![value respondsToSelector: @selector(floatValue)])
{
[NSException raise: NSInternalInconsistencyException
format: @"Value (%@) does not respond to -floatValue.",
[value class]];
return nil;
}
return [NSNumber numberWithFloat: [value floatValue]/10000.];
}
@end
/**********************************************************
* @implementation Controller
*/
@interface Controller ()
@property (retain,readwrite) NSString * outputFolderPath;
@end
@implementation Controller
- (id)init
{
if(self = [super init])
{
[self bind:@"outputFolderPath" toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.outputFolderPath" options:nil];
[[[NSUserDefaultsController sharedUserDefaultsController] values] bind:@"outputFolderPath" toObject:self
withKeyPath:@"outputFolderPath" options:nil];
VLCFloat10000FoldTransformer *float100fold;
float100fold = [[[VLCFloat10000FoldTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:(id)float100fold forName:@"Float10000FoldTransformer"];
self.media = nil;
self.streamSession = nil;
selectedStreamOutput = [[NSNumber alloc] initWithInt:0];
self.remoteURLAsString = [NSString stringWithString:@"http://youtube.com/watch?v=IXpx2OEWBdA&feature=bz303"];
outputFilePath = nil;
if( !self.outputFolderPath || [self.outputFolderPath isKindOfClass:[NSNull class]])
self.outputFolderPath = [@"~/Movies/Flash Video Converted" stringByExpandingTildeInPath];
}
return self;
}
- (void)dealloc
{
[outputFilePath release];
[remoteURLAsString release];
[streamSession release];
[media release];
[super dealloc];
}
@synthesize streamSession;
@synthesize selectedStreamOutput;
@synthesize media;
@synthesize outputFolderPath;
- (void)awakeFromNib
{
[window setShowsResizeIndicator:NO];
[NSApp setDelegate: self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[VLCLibrary sharedLibrary];
}
- (NSString *)remoteURLAsString
{
return remoteURLAsString;
}
- (void)setRemoteURLAsString:(NSString *)newURLAsString
{
[remoteURLAsString release];
remoteURLAsString = [[newURLAsString copy] retain];
media = [[newURLAsString copy] retain];
[self setMedia:[VLCMedia mediaWithPath:newURLAsString]];
}
+ (NSSet *)keyPathsForValuesAffectingOutputFilePath
{
return [NSSet setWithObjects:@"media.metaDictionary.title", nil];
}
- (void)freezeOutputFilePath
{
[outputFilePath release];
outputFilePath = nil;
outputFilePath = [self outputFilePath];
[outputFilePath retain];
}
- (NSString *)outputFilePath
{
if(outputFilePath)
return [outputFilePath copy];
VLCMedia * aMedia = self.streamSession ? self.streamSession.media ? self.streamSession.media : self.media : self.media;
NSString * name = [[[aMedia metaDictionary] objectForKey:@"title"] lastPathComponent];
NSString * extension = [selectedStreamOutput intValue] == 2 ? @"mpeg" : @"mp4";
NSString * path = [NSString stringWithFormat:@"%@/%@.%@", self.outputFolderPath, name, extension ];
int i;
for( i = 0; [[NSFileManager defaultManager] fileExistsAtPath:path]; i ++)
{
path = [NSString stringWithFormat:@"%@/%@ %d.%@", self.outputFolderPath, name, i, extension ];
if( i > 256 )
{
/* Don't got too far */
/* FIXME: Be nicer with the user and give him a choice for the new name */
NSRunAlertPanelRelativeToWindow(@"File already exists",
[NSString stringWithFormat:
@"File '%@', already exists. The old one will be deleted when the OK button will be pressed", path],
@"OK", nil, nil, window);
break;
}
}
return path;
}
- (IBAction)convert:(id)sender
{
VLCStreamOutput * streamOutput;
[self.streamSession removeObserver:self forKeyPath:@"isComplete"];
self.streamSession = [VLCStreamSession streamSession];
[self freezeOutputFilePath];
if([selectedStreamOutput intValue] == 2)
{
streamOutput = [VLCStreamOutput mpeg2StreamOutputWithFilePath:[self outputFilePath]];
}
else if([selectedStreamOutput intValue] == 1)
{
streamOutput = [VLCStreamOutput mpeg4StreamOutputWithFilePath:[self outputFilePath]];
}
else
streamOutput = [VLCStreamOutput ipodStreamOutputWithFilePath:[self outputFilePath]];
/* Make sure we are exporting to a well known directory */
[[NSFileManager defaultManager] createDirectoryAtPath:self.outputFolderPath attributes:nil];
[self.streamSession setStreamOutput:streamOutput];
[self.streamSession setMedia:self.media];
[self.streamSession startStreaming];
[self.streamSession addObserver:self forKeyPath:@"isComplete" options:NSKeyValueObservingOptionNew context:nil];
/* Show the new view */
[[window contentView] addSubview:workingView];
NSRect frame = [workingView frame];
frame.origin.y -= NSHeight([window contentRectForFrameRect:[window frame]]) + 20.f;
[workingView setFrame:frame];
[[window animator] setFrame:NSMakeRect([window frame].origin.x, [window frame].origin.y-NSHeight([workingView frame]), NSWidth([window frame]), NSHeight([window frame])+NSHeight([workingView frame])) display:YES];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"isComplete"])
{
if([self.streamSession isComplete])
{
/* Notify the user */
[[NSSound soundNamed:@"Glass"] play];
/* Set the icon */
[openConvertedFileButton setImage:[[NSWorkspace sharedWorkspace] iconForFile:[self outputFilePath]]];
/* Rename the link with a nicer name */
NSString * oldPath = [self outputFilePath];
[self freezeOutputFilePath];
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:[self outputFilePath] error:NULL];
}
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (IBAction)openConvertedFile:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:[self outputFilePath]];
}
- (IBAction)pickOutputFolderPath:(id)sender;
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:NO];
[panel beginSheetForDirectory:self.outputFolderPath file:nil types:nil modalForWindow:[sender window] modalDelegate:self didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
- (void)openPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if(returnCode != NSOKButton || ![[panel filenames] count])
return;
self.outputFolderPath = [[panel filenames] objectAtIndex:0];
}
- (IBAction)openConvertedEnclosingFolder:(id)sender
{
[[NSWorkspace sharedWorkspace] selectFile:[self outputFilePath] inFileViewerRootedAtPath:[[self outputFilePath] stringByDeletingLastPathComponent]];
}
- (IBAction)cancel:(id)sender
{
[self.streamSession stop];
}
@end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IBClasses</key>
<array>
<dict>
<key>CLASS</key>
<string>GradientBackgroundView</string>
<key>LANGUAGE</key>
<string>ObjC</string>
<key>SUPERCLASS</key>
<string>NSImageView</string>
</dict>
<dict>
<key>CLASS</key>
<string>FirstResponder</string>
<key>LANGUAGE</key>
<string>ObjC</string>
<key>SUPERCLASS</key>
<string>NSObject</string>
</dict>
<dict>
<key>ACTIONS</key>
<dict>
<key>convert</key>
<string>id</string>
<key>openConvertedEnclosingFolder</key>
<string>id</string>
<key>openConvertedFile</key>
<string>id</string>
<key>pickOutputFolderPath</key>
<string>id</string>
</dict>
<key>CLASS</key>
<string>Controller</string>
<key>LANGUAGE</key>
<string>ObjC</string>
<key>OUTLETS</key>
<dict>
<key>openConvertedFileButton</key>
<string>NSButton</string>
<key>remoteURLView</key>
<string>NSView</string>
<key>titleView</key>
<string>NSView</string>
<key>window</key>
<string>NSWindow</string>
<key>workingView</key>
<string>NSView</string>
</dict>
<key>SUPERCLASS</key>
<string>NSObject</string>
</dict>
</array>
<key>IBVersion</key>
<string>1</string>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IBFramework Version</key>
<string>629</string>
<key>IBLastKnownRelativeProjectPath</key>
<string>../../FlashVideoDownloader.xcodeproj</string>
<key>IBOldestOS</key>
<integer>5</integer>
<key>IBOpenObjects</key>
<array>
<integer>21</integer>
<integer>169</integer>
<integer>940</integer>
<integer>704</integer>
</array>
<key>IBSystem Version</key>
<string>9B18</string>
<key>targetFramework</key>
<string>IBCocoaFramework</string>
</dict>
</plist>
//
// Prefix header for all source files of the 'test' target in the 'test' project
//
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
//
// GradientBackgroundView.h
// iPodConverter
//
// Created by Pierre d'Herbemont on 1/12/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface GradientBackgroundView : NSImageView {
}
@end
//
// GradientBackgroundView.m
// iPodConverter
//
// Created by Pierre d'Herbemont on 1/12/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "GradientBackgroundView.h"
/**********************************************************
* Why not drawing something nice?
*/
@implementation GradientBackgroundView
- (void)awakeFromNib
{
/* Buggy nib files... Force us to be on the back of the view hierarchy */
NSView * superView;
[self retain];
superView = [self superview];
[self removeFromSuperview];
[superView addSubview:self positioned: NSWindowBelow relativeTo:nil];
}
- (void)drawRect:(NSRect)rect
{
NSColor * topGradient = [NSColor colorWithCalibratedWhite:.12f alpha:1.0];
NSColor * bottomGradient = [NSColor colorWithCalibratedWhite:0.55f alpha:0.9];
NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations:bottomGradient, 0.f, bottomGradient, 0.1f, topGradient, 1.f, nil];
[gradient drawInRect:self.bounds angle:90.0];
[super drawRect:rect];
}
@end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>Flash Video Downloader</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>org.videolan.flashvideodownloader</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Flash Video Downloader</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
//
// main.m
// test
//
// Created by Pierre d'Herbemont on 13/04/07.
// Copyright __MyCompanyName__ 2007. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <VLCKit/VLCKit.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment