AppleScript C Perl Shell Xcode Other

Uploading movies to YouTube - the easy way

Post Reply
coding / applescript     Views: 2726Prev .. Next
Uploading movies to YouTube - the easy wayPosted: Thursday, June 27, 2013 [10:02:24] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 356
If you need to upload multiple files to YouTube - it can take time. If movies are on the same subject, educational or on any other subject that use a lot of repetition it may be easier to let program do the uploading for you.
Program below takes compiled .TXT files with Title, Description and Keywords and a movie file. File Names for .MOV and .TXT files are the same except file extension. Use lower case for file extensions though.
All .TXT files can be compiled out of database and movies cut by QTCoffee http://www.3am.pair.com/QTCoffee.html program.

The program is an AppleScript written on-the-fly by Perl and run from the Perl program. Perl program run from Terminal.
Here is the code:
View Code#!/usr/bin/perl
####################################################################################
## This program is written to save time uploading multiple files
## Do not use it unless you fully aware of YouTube policies.
## Your YouTube account could be terminated for abuse because of use of this program
## Code posted for educational purposes only
## Use it at your own risk!
## Author is not responsible for misuse of the code or any portion of the code
####################################################################################
## Set folder for .mov files - just move the folder onto Terminal window
print "Choose upload folder: ";
$folder = <STDIN>;chomp $folder;$folder =~ s/\s+$//;$folder =~ s/\/+$//;
unless(-d $folder) {print "No folder: $folder found\n";exit(0);}
## Choose ho many files to upload
print "How many files to process ";
$flprocc = <STDIN>;chomp $flprocc;$flprocc =~ s/\D//;
if($flprocc > 1) {$flprocc--;}

&getcode;

@all = glob("$folder/*");
randomaze( \@all ); ## All files in upload folder - shuffle array of files (optional)
foreach $f (@all) {
unless($f =~ m/\.mov$/i) {next;}
## Now calculate how big are movie file and add time for upload
$flsz = (stat ("$f"))[7];
$flsz /= (1024 * 1024);$flsz = sprintf("%0.2f",$flsz);
print "File: Size: $flsz\n";$addtime='';
## keep in-mind - delay depends on your Internet connection speed and a uploaded file size
if($flsz > 4) {$addtime = $flsz - 4;$addtime++;$addtime = sprintf("%0.0f",$addtime);$addtime *= 6;$addtime = "\ndelay $addtime\t\-\- Size: $flsz Mb";}
else {$addtime = "\n\-\- Size: $flsz Mb";}
$ft=$f;$ft =~ s#\.mov$#\.txt#;
$tot++;
unless(-f $ft) {print "\t$tot\. Support file $ft not present -> next\n";next;}
$totd++;$addtocode='';
if($totd < 2) { ## On first upload make Safari choose the folder
$addtocode = "\t\t\t\t\tset x to \"$folder\"
\t\t\t\t\tkeystroke \"g\" using {command down, shift down}\n\t\t\t\t\tdelay 1\n\t\t\t\t\tkeystroke x
\t\t\t\t\tdelay 1\n\t\t\t\t\tkeystroke return\n\t\t\t\t\tdelay 1\n";
} ## END FIRST RECORD

## Now read the source text file for movie title, description and some keywords
## TXT file has to be named as movie file with .txt extension
## in my case title, description and keywords are separated by double newline - it could be anything just do not forget to replace it on the next line
$d = `cat $ft`;@lnz = split(/\n\n/,$d);$d='';
## Do some text replacement if needed
$kwd = pop(@lnz);$title=shift(@lnz);$text = join "\n\n", @lnz;$kwd =~ s#\_# #g;$kwd .= ',';

## Make sure title is not too long or movie will be rejected by YouTube
$title =~ s/\,/ /g;$title =~ tr/ //s;
if(length($title) > 70) {
@ghtr = split(/ /,$title);$title='';$ssccma='';
foreach $hdprt (@ghtr) {
$title .= "$ssccma$hdprt";$ssccma=' ';
if(length($title) > 65) {last;}
} ## FOREACH END
@ghtr=();$ssccma='';
} ## TITLE LONDER THAN 70

$ttllngth=length($title);
$text =~ s/\n/\n\r/g;
print "\n$tot\. ($totd) $f\n________________________________________\n";
print "TITLE: $title\nTEXT: $text\nKEYWORDS: $kwd\n"; ## Print some statistics in Terminal window
@fnmz = split(/\//,$f);$fname = pop(@fnmz);@fnmz=();
$tocode1 = "\t\t\t\t\tkeystroke \"$fname\"\n\t\t\t\t\tdelay 1\n\t\t\t\t\tkeystroke return\n";
$codeuse = $code;$codeuse =~ s#!CODE1!#$addtocode$tocode1#;
$title =~ s#\"#\\\"#g;$text =~ s#\"#\\\"#g;$kwd =~ s#\"#\\\"#g;
## This code will enter Title, Description and Keywords into YouTube form
## The only thing for you to do is to choose proper Category and it will stick for the rest of the movies
$printcode = "\t\t\t\t\tkeystroke \"a\" using {command down}\n\t\t\t\t\tdelay 1
\t\t\t\t\tkeystroke \"$title\"\n\t\t\t\t\tdelay 1\n\t\t\t\t\tkeystroke tab\t
\t\t\t\t\tkeystroke \"$text\"\n\t\t\t\t\tdelay 1\n\t\t\t\t\tkeystroke tab\t\n\t\t\t\t\tdelay 1\n\t\t\t\t\tkeystroke tab\t
\t\t\t\t\tkeystroke \"$kwd\"\n\t\t\t\t\tdelay 1";
$codeuse =~ s#!CODE2!#$printcode#;
$codeuse =~ s#!ADDTIME!#$addtime#;
unless(-d "$folder/codes") {`mkdir $folder/codes`;}
unless(-d "$folder/done") {`mkdir $folder/done`;}
$fnamecode=$fname;$fnamecode =~ s/\.\w{3}$/\.scpt/;

## Now move the .mov and .txt files to "done" folder
## if anything goes wrong - you can move them back and re-run the program
## it is better if AppleScript will do it ensuring AppleScript was fired
## if not - files will remain in a main folder
$manmvv = $f;$manmvv =~ s#$folder/##;
$toscript = "do shell script \"mv $f $folder/done/$manmvv\"\n";
$toscript .= "do shell script \"touch $folder/done/$manmvv\"\n";

$manmvv = $ft;$manmvv =~ s#$folder/##;
$toscript .= "do shell script \"mv $ft $folder/done/$manmvv\"\n";
$toscript .= "do shell script \"touch $folder/done/$manmvv\"\n";
$codeuse .= "\n$toscript";

## now safe the AppleScript code and run it
open(SCP,">$folder/codes/$fnamecode");print SCP $codeuse;close(SCP);
`osascript $folder/codes/$fnamecode`;sleep(1);

## See how many movies done and stop if pre-set number was reached
if($totd > $flprocc) {exit(0);}
} ## FOREACH END

## Sub - get AppleScript code
sub getcode {
$code = 'tell application "Safari"
activate
make new document
delay 1
-- this is YouTube URL for upload. First you have to manually log-in to your YouTube account in Safari
-- and get to this URL manually - so Safari will can cache it for faster load
set URL of document 1 to "https://www.youtube.com/my_videos_upload"
set myURL to URL of document 1
delay 4
set web_page_is_loaded to false
set myCounter to 0
set maxCounter to 30
set my_delay to 1
repeat until web_page_is_loaded is true
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" then
delay my_delay
else
set web_page_is_loaded to true
end if
set myCounter to myCounter + 1
if myCounter is maxCounter then
set web_page_is_loaded to true
end if
end repeat

delay 1

-- here program finds the "File" field and clicks it for upload
set LinkPresent to ""
set LinkPresent to (do "document.getElementsByTagName(\'input\').length" in document 1)
set LinkPresent to LinkPresent - 1
repeat with n from 0 to LinkPresent
set elemType to (do "document.getElementsByTagName(\'input\')[" & n & "].type" in document 1)
set elemName to (do "document.getElementsByTagName(\'input\')[" & n & "].name" in document 1)
if elemType contains "file" then
do "document.getElementsByTagName(\'input\')[" & n & "].click()" in document 1
delay 1
tell application "System Events"
tell process "Safari"
-- program will choose the file to upload from the list of the files in your folder here
!CODE1!
end tell
end tell
exit repeat
end if
end repeat
-- choose delay here according to your movie size and internet connection speed
-- delay can also be configured in above Perl code
delay 5
-- now program finds the "title" field and fills it out
-- along with description and keywords fields
repeat with n from 0 to LinkPresent
set elemType to (do "document.getElementsByTagName(\'input\')[" & n & "].type" in document 1)
set elemName to (do "document.getElementsByTagName(\'input\')[" & n & "].name" in document 1)
if elemName contains "title" then
do "document.getElementsByTagName(\'input\')[" & n & "].focus()" in document 1
delay 1
tell application "System Events"
tell process "Safari"
!CODE2! -- This will be replaced by code provided by the Perl
end tell
end tell
exit repeat
end if
end repeat

end tell -- end Safari tell
delay 30!ADDTIME! -- additional delay replacement here based on uploaded file size
-- close Safari window
tell application "System Events"
tell process "Safari"
keystroke "w" using {command down}
end tell
end tell
';
} ## END SUB GET CODE

sub randomaze {
my $array = shift;
my $i;
for ($i = @$array; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j] = @$array[$j,$i];
}
return @array;
}

Enjoy!There's no place like ~
coding / applescriptPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group