且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个Java开发的在线视频下载工具,可以把视频分块下载到本地,再拼凑成完整版

更新时间:2022-09-12 15:56:11

package flick;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DownloadLauncher {

    static final public int LAST = 109;
    static final public int START = 1;
    static final public String PREFIX = "https://cfvod.kaltura.com/scf/hls/p/1921661/sp/192166100/serveFlavor/entryId/1_e4vktn7o/v/1/ev/7/flavorId/1_51i2iaqb/name/a.mp4/seg-";


    static final public String POSTFIX1 = "-v1-a1.ts?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6Ly9jZnZvZC5rYWx0dXJhLmNvbS9zY2YvaGxzL3AvMTkyMTY2MS9zcC8xOTIxNjYxMDAvc2VydmVGbGF2b3IvZW50cnlJZC8xX2U0dmt0bjdvL3YvMS9ldi83L2ZsYXZvcklkLzFfNTFpMmlhcWIvbmFtZS9hLm1wNC8qIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNTQ0NTExMjI4fX19XX0_&Signature=ZvSmUm7fUqvB7mX2XyrsMozNlRDL7O0QDkKTG5te-NDPNPvc1y7m~q20lAFYk2t0UzCOHSdpF5Jms3B65QPXG2uOF94eBQ8QnNAsUcCDyeFScwLo2b1WNXPnjn18cSZYXH2sBf9TiCu8TrMTWlf6SYGnhtWxQQfzdjbyVWhC9Lpt0uDL8gE~xpxNRUADRIJL7wQru4eBPWbqUFNkP6nNeTTkfcH~y-cYR1kZW8IaYoh1JRLTnZUrMjQMbyC6zi4MExo-SmAErLBXrjmjgMJ1fK4f9yRzIqlXnwIfg60jPDP-pBCvIGtVV22F0SLisaBrfHRCjSuOnZ0r0NvtcYJLZQ__&Key-Pair-Id=APKAJT6QIWSKVYK3V34A";
    static final public String POSTFIX = ".ts";
    static final public String LOCALPATH = "c:\\temp\\";
    static final public String MERGED = "merged.avi";
    
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        
        for( int i = START; i <= LAST; i++ ){
            // String task = PREFIX + i + POSTFIX;
            String task = PREFIX + i + POSTFIX1;
            VideoFragmentDownloader load = new VideoFragmentDownloader(task, i);
            executor.execute(load);
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
        }

    }

}

package flick;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Merger {

    private static final int START = 1;
    private static void run() throws IOException{
        
        FileInputStream in = null;
        String destFile = DownloadLauncher.LOCALPATH + 
                DownloadLauncher.MERGED;
        
        FileOutputStream out = new FileOutputStream(destFile,true);
        
        for( int i = START; i <= DownloadLauncher.LAST; i++){
            byte[] buf = new byte[1024];
            int len = 0;
            String sourceFile = DownloadLauncher.LOCALPATH + i + 
                    DownloadLauncher.POSTFIX;
            
            in = new FileInputStream(sourceFile);
    
            while( (len = in.read(buf)) != -1 ){
                out.write(buf,0,len);
            }
        }
        out.close();
    }
    
    public static void main(String[] args) {
        try {
            run();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Merged ok!");
    }

}
package flick;

import java.util.ArrayList;
import java.util.List;


public class TEST
{
    public static void main(String[] args)
    {
        Utility.SetProxy();
        ArrayList<String> a = null;
        List<String> urlList = Utility.getURLList("c:\\temp\\url.txt");
        for(int i = 0; i < urlList.size(); i++)
            System.out.println("Index: " + i + "-" + URLFetcher.getStaticURL(urlList.get(i)));
    }
}

package flick;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLFetcher 
{
    static public String getStaticURL(String ID)
    {
        String strFullURL = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=b29c2c7f2a7cf6deccdd2672a7893025&photo_id="
                + ID + "&format=rest";
        URL url;
        StringBuffer sb = new StringBuffer();
        try 
        {
            url = new URL(strFullURL);
            URLConnection urlConnection;
            urlConnection = url.openConnection(); 
            urlConnection.setRequestProperty("Accept-Charset", "UTF-8"); 
            InputStream is = urlConnection.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int numCharsRead;
            char[] charArray = new char[1024];
            while ((numCharsRead = in.read(charArray)) > 0) 
            {
                sb.append(charArray, 0, numCharsRead);
            }
            in.close();
        }
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        return Utility.getStaticURL(sb.toString());
    }
}
package flick;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class Utility
{
    static public void SetProxy()
    {
        System.setProperty("http.proxyHost", "proxy");
        System.setProperty("http.proxyPort", "8080");
        System.setProperty("https.proxyHost", "proxy");
        System.setProperty("https.proxyPort", "8080");
    }
    
    static public List<String> getURLList(String file)
    {
        List<String> urlList = new ArrayList<String>();
        try 
        {
            FileInputStream f = new FileInputStream(file);
            BufferedReader dr = new BufferedReader(new InputStreamReader(f));   
            String url = null;
            while ( ( url = dr.readLine() ) != null )
            {
                urlList.add(getID(url));
            }
            f.close();
            dr.close();
        } 
        catch (IOException e) 
        {
        }
        return urlList;
    }
    
    static private String getID(String url)
    {
        String[] a = url.split("/");
        return a[a.length-1];
    }
    
    static public String getStaticURL(String content)
    {
        String prefix = "source=";
        int index = content.indexOf("Original");
        if( index == -1)
            return null;
        String sub = content.substring(index);
        int source = sub.indexOf(prefix);
        if( source == -1)
            return null;
        int end = sub.indexOf("url=");
        if( end == -1)
            return null;
        return sub.substring(source + prefix.length(), end);
    }
}

package flick;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class VideoFragmentDownloader  implements Runnable{
    private String mTask;
    private int mIndex;
    
    public  VideoFragmentDownloader(String url, int index){
        this.mTask = url;
        this.mIndex = index;
    }
    
    private void download(){
        URL task = null;
        String path = DownloadLauncher.LOCALPATH + this.mIndex + 
                DownloadLauncher.POSTFIX;
        String url = this.mTask;
        try {
            task = new URL(url);
            DataInputStream dataInputStream = new DataInputStream(task.openStream());
             
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
 
            byte[] buffer = new byte[1024];
            int length;
 
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            System.out.println("File: " + this.mIndex + " downloaded ok");
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    @Override
    public void run() {
        this.download();
    }

}