Java 동영상 재생시간 구하기 - java dong-yeongsang jaesaengsigan guhagi

/*

HLS VOD 재생시간이 맞질않아 재생가능한 토큰값 붙은 URL호출시 

playList.m3u8 파일이 다운로드 된다. 다운받은 파일안 URL을 찾아서 호출하면

chunkList.m3u8 파일이 다운로드 된다. 그 파일

#EXTINF:11.779, 값들을 더하면 정확한 재생시간이 구해짐.

*/

import java.io.*;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

public class  M3U8Parse

{

/* main */

public static void main(String[] args) 

{

System.out.println("M3U8 Pasing Start!");

// 다운로드할 URL 주소

String playListUrl = "//fs1024.poo%%%%%중간생략%%%/playlist.m3u8?oken=1feba457a43f8378b993cf379d27cc5f&expr=53cf9db3";

// 생성될 M3U8 파일경로,파일명 (한번 파싱하고 버릴것이므로, 여러개 생성안되게 같은 파일명 playlist.chunkList. 을 1.txt 으로 실행시 overWrite함.

String sourceFile = "c:/1/1.txt";

// Step1 : 파일 다운로드 playlist.m3u8(= 1.txt)

String playListFile = httpDownload(playListUrl, sourceFile);

// Step2 : 다운받은 playlist.m3u8 안 chunklist.m3u8 URL

String chunkListUrl = httpSelector(playListFile);

// Step3 : 파일다운로드  chunkList.m3u8(= 1.txt);

String chunkListFile = httpDownload(chunkListUrl,sourceFile);

// chunkList.M3U8 (1.txt) 파싱 해서 재생시간 구하기

Float resultDurationValue = durationSum(chunkListFile);

System.out.println("재생시간(초) :"+resultDurationValue);

}

/* download */

    public static String httpDownload(String sourceUrl, String sourceFile) {

        FileOutputStream fos = null;

        InputStream is = null;

String targetUrl = sourceUrl;

String targetFilename = sourceFile;

        try {

fos = new FileOutputStream(targetFilename); 

            URL url = new URL(targetUrl);

            URLConnection urlConnection = url.openConnection();

            is = urlConnection.getInputStream();

            byte[] buffer = new byte[1024];

            int readBytes;

            while ((readBytes = is.read(buffer)) != -1) {

                fos.write(buffer, 0, readBytes);

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally { 

            try {

                if (fos != null) {

                    fos.close();

                }

                if (is != null) {

                    is.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

return targetFilename;

    }

/* playlist.M3U8(1.txt) 안 Http 주소 가져오기 */

public static String httpSelector(String resultFile){

StringBuffer sb = new StringBuffer();

FileReader readFile; 

BufferedReader br; 

String getLine; 

String resultUrl = "";

try { 

 readFile = new FileReader(resultFile);// 파일경로

 br = new BufferedReader(readFile);   

 while((getLine = br.readLine()) != null){// 한줄씩 읽기 (마지막줄 체크)

if(getLine.startsWith("http")){// 파일중 http 로 시작하는 줄 찾아서 저장.

resultUrl = getLine;

}

 } 

 } catch (FileNotFoundException e) { 

 e.printStackTrace(); 

 } catch (IOException e) { 

 e.printStackTrace(); 

return resultUrl;

}

/* chunkList.M3U8(1.txt) 안 duration값 더하기 */

public static Float durationSum(String resultUrl){

StringBuffer sb = new StringBuffer();

FileReader readFile; 

BufferedReader br; 

String getLine; 

Float resultValue = 0f;

try { 

 readFile = new FileReader(resultUrl);// 파일경로

 br = new BufferedReader(readFile);   

 while((getLine = br.readLine()) != null){ // 한줄씩 읽기 (마지막줄 체크)

 if(getLine.startsWith("#EXTINF:")){

int startIndex = getLine.indexOf(":");

int endIndex = getLine.indexOf(",");

resultValue += Float.parseFloat(getLine.substring(startIndex+1, endIndex));

}

 } 

 } catch (FileNotFoundException e) { 

 e.printStackTrace(); 

 } catch (IOException e) { 

 e.printStackTrace(); 

return resultValue;

}

}// end of Class M3U8Parse

Toplist

최신 우편물

태그