Code covered by the BSD License  

Highlights from
Upload a Video to YouTube™

image thumbnail
from Upload a Video to YouTube™ by Matthew Simoneau
Using the Youtube™ data API, upload a video directly from MATLAB®.

youtubeUpload(file,title,description,category,keywords)
function id = youtubeUpload(file,title,description,category,keywords)
%youtubeUpload(file,title,description,category,keywords)
%   All of the arguments have lame defaults.  "keywords" is a cell array.
%
%   It may take several hours for the video to appear on the site.  You can
%   check the status at <http://www.youtube.com/my_videos>.  They the video
%   will be at the URL <http://www.youtube.com/watch?v=> plus the ID
%   returned by the function.

%   Matthew J. Simoneau
%   Copyright 2008 The MathWorks, Inc. 

    % Import the required classes.
    import com.google.gdata.data.youtube.*;
    import com.google.gdata.data.media.mediarss.*;
    import com.google.gdata.data.media.*;
    import com.google.gdata.client.youtube.*;

    if (nargin < 2)
        title = 'test';
    end
    if (nargin < 3)
        description = 'test';
    end
    if (nargin < 4)
        category = 'Tech';
    end
    if (nargin < 5)
        keywords = {'MATLAB'};
    end
    
    % Define needed variables.
    username = getPrefOrPrompt('Username');
    password = getPrefOrPrompt('Password');
    developerKey = 'AI39si7J7QPwNZriOcSa6z2wpbYbN2pFgI9Xdvv4EYSq84Ss3EclSWorSJ4Dtqr2gLyXPSGsDlGyldEsGa9pB_UwNmaXYiCVXA';
    clientid = 'ytapi-MatthewSimoneau-YouTubeUploadfro-ge7j8nsn-0';

    % Locate the file to upload.
    videoFile = java.io.File(file);
    if ~videoFile.exists()
        error('Can''t find file.')
    end
    
    % Create an entry to upload.
    try
        newEntry = VideoEntry;
    catch e
        switch e.identifier
            case 'MATLAB:UndefinedFunction'
                msg = 'Add "gdata-youtube-1.0.jar" to your "classpath.txt", and be sure "gdata-client-1.0.jar", "gdata-media-1.0.jar", "gdata-client-meta-1.0.jar", and "gdata-core-1.0.jar" are in the same directory.';
                error(msg)
            otherwise
                rethrow(e)
        end
    end
    
    % Define the entry's MediaGroup.
    mg = newEntry.getOrCreateMediaGroup;
    mg.addCategory(MediaCategory(YouTubeNamespace.CATEGORY_SCHEME,category));
    mg.setTitle(MediaTitle());
    mg.getTitle().setPlainTextContent(title);
    mg.setKeywords(MediaKeywords());
    for iKeywords = 1:numel(keywords)
        mg.getKeywords().addKeyword(keywords{iKeywords});
    end
    mg.setDescription(MediaDescription());
    mg.getDescription().setPlainTextContent(description);

    % Define the entry's MediaSource.
    ms = MediaFileSource(videoFile,'video/avi');
    newEntry.setMediaSource(ms);

    % Upload the file.
    service = YouTubeService(clientid,developerKey);
    service.setUserCredentials(username, password)    
    videoUploadFeed = ['http://uploads.gdata.youtube.com/feeds/api/users/' username '/uploads'];
    ne = service.insert(java.net.URL(videoUploadFeed),newEntry);
    id = strrep(char(ne.getId),'http://gdata.youtube.com/feeds/api/videos/','');
end

function value = getPrefOrPrompt(param)
    value = getpref('Youtube',param,'');
    while isempty(value)
        valueCell = inputdlg(param);
        value = valueCell{1};
        setpref('Youtube',param,value);
    end
end

Contact us at files@mathworks.com