com.brightcove.zartan.encoding.test.ZencodeTest.java Source code

Java tutorial

Introduction

Here is the source code for com.brightcove.zartan.encoding.test.ZencodeTest.java

Source

package com.brightcove.zartan.encoding.test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test;

import com.brightcove.common.io.FTPHelper;
import com.brightcove.common.io.FTPHelperBuilder;
import com.brightcove.zartan.common.account.Account;
import com.brightcove.zartan.common.account.Credentials;
import com.brightcove.zartan.common.account.User;
import com.brightcove.zartan.common.account.ZencoderCredentials;
import com.brightcove.zartan.common.encode.TranscodeInfo;
import com.brightcove.zartan.common.encode.TranscodeOptionGroup;
import com.brightcove.zartan.common.environment.TranscodeEnvironment;
import com.brightcove.zartan.common.file.ExtensionFilenameFilter;
import com.brightcove.zartan.common.file.FileFinder;
import com.brightcove.zartan.common.file.TranscodedVideoFile;
import com.brightcove.zartan.common.file.VideoFileInfo;
import com.brightcove.zartan.common.file.VideoFileInfoRegistry;
import com.brightcove.zartan.common.helper.AccountHelper;
import com.brightcove.zartan.common.verifier.TrancodeResponse;
import com.brightcove.zartan.common.verifier.Verifiable;
import com.brightcove.zartan.common.verifier.VerifiableSerializer;
import com.brightcove.zartan.common.verifier.VerifiableTranscode;
import com.brightcove.zartan.encode.TencodeAPI;
import com.brightcove.zartan.encode.ZencodeAPI;

/**
 * Copyright (C) 2012 Brightcove Inc. All Rights Reserved. No use, copying or distribution of this
 * work may be made except in accordance with a valid license agreement from Brightcove Inc. This
 * notice must be included on all copies, modifications and derivatives of this work.
 * 
 * Brightcove Inc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BRIGHTCOVE SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * "Brightcove" is a registered trademark of Brightcove Inc.
 */

public class ZencodeTest {
    //@Test
    public void testZencode() throws Throwable {
        VerifiableSerializer vser = new VerifiableSerializer();
        ZencodeAPI zapi = new ZencodeAPI();
        User u = new User("storpey@brightcove.com", "Password!", true);
        List<Credentials> creds = new ArrayList<Credentials>();
        creds.add(new ZencoderCredentials("716a8085f2094a65d8688d06d2637364"));
        Account acc = new Account(u, 123l, creds);
        AccountHelper ah = new AccountHelper();
        ArrayList<VideoFileInfo> files = getFils();
        TencodeAPI tapi = new TencodeAPI();
        User tu = new User("storpey@brightcove.com", "Password!", true);

        Account tacc = new Account(u, 123l, null);
        TranscodeEnvironment env = new TranscodeEnvironment("zen-prod", "https://app.zencoder.com/api/v2/jobs");

        TranscodeEnvironment tenv = new TranscodeEnvironment("tencode",
                "http://golftrans02.qanet.local:23080/tencode/encode/submit");
        FTPHelperBuilder ftpHB = new FTPHelperBuilder("10.1.11.139", "qa", "Passw0rd!",
                FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
        FTPHelper ftpH = ftpHB.createFTPHelper();
        for (VideoFileInfo file : files) {

            TranscodeInfo transcode = new TranscodeInfo(
                    new TranscodeOptionGroup(ah.getDefaultTranscodeOptions(), "Default Transcode Options"), file);

            Verifiable vt = zapi.submitTranscode(transcode, env, acc);
            vser.addVerifiable(vt);

            TranscodeInfo ttranscode = new TranscodeInfo(
                    new TranscodeOptionGroup(ah.getDefaultTranscodeOptions(), "Default Transcode Options"), file);

            tapi.submitTranscode(ttranscode, tenv, tacc);
        }
        try {
            vser.serializeToFile("test.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
        ArrayList<String> haveSeen = new ArrayList<String>();
        VerifiableSerializer v = VerifiableSerializer.deserializeFromFile("test.json");
        //TranscodeVerifierRunner tvr= new TranscodeVerifierRunner();
        for (Verifiable verf : v.getVerifiableList()) {
            if (verf instanceof VerifiableTranscode) {
                VerifiableTranscode tverf = (VerifiableTranscode) verf;
                TrancodeResponse response = new TrancodeResponse();
                for (TranscodedVideoFile tf : ((VerifiableTranscode) verf).getTranscodedVideoFiles()) {
                    response.addTranscodedVideoFile(tf);
                    String location = response.getFileLocation(tf);
                    if (!haveSeen.contains(location)) {
                        String location2 = "/zencoder/output-" + tf.getMatchingRequest().getOptionId()
                                + tverf.getTranscodeInfo().getFile().getFileName() + ".mp4";
                        ftpH.connect();
                        ftpH.put("/", location2, new File(location));
                        ftpH.disconnect();
                        haveSeen.add(location);
                        System.out.println(location + " , " + location2);
                    }

                }
                //tvr.runVerifiers(tverf, response);
            }
        }
        ftpH.disconnect();
    }

    private ArrayList<VideoFileInfo> getFils() {
        ArrayList<VideoFileInfo> res = new ArrayList<VideoFileInfo>();
        VideoFileInfoRegistry reg = VideoFileInfoRegistry.getInstance();
        VideoFileInfo vfi = reg
                .getInfoforFilename("/mnt/transnas1/938689400001_1638424506001_Attack-The-Block-VOSE.mp4");
        vfi.setHttpLocation(
                "http://brightcove01q.brightcove.com/systemtests/zencoder/938689400001_1638424506001_Attack-The-Block-VOSE.mp4");
        res.add(vfi);

        //        
        //        for(String a:f.list(ExtensionFilenameFilter.VIDEO_FILENAME_FILTER)){
        //            VideoFileInfo vfi=reg.getInfoforFilename("/mnt/transnas1/TestFiles/"+a);
        //            vfi.setHttpLocation("http://brightcove01q.brightcove.com/systemtests/zencoder/"+a);
        //            res.add(vfi);
        //        }
        reg.saveRegistry();

        return res;
    }

}