/*

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 = "http://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


/* m3u8 파일 리딩 : URL  */


import java.io.*;


public class TextFileReading

{

public static void main(String[] args){


StringBuffer sb = new StringBuffer();

FileReader readFile; 

BufferedReader br; 


String getLine; 


try { 

 readFile = new FileReader("C:/1/1.txt"); // 파일경로

 br = new BufferedReader(readFile);   

 

 String resultUrl = "";


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

     

System.out.println(getLine);


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

resultUrl = getLine;

}


 } 

  System.out.println("URL :"+ resultUrl);

 } catch (FileNotFoundException e) { 

 e.printStackTrace(); 

 } catch (IOException e) { 

 e.printStackTrace(); 

}

'JAVA' 카테고리의 다른 글

자바에서의 인스턴스란?  (0) 2014.07.24
3. 재생시간 구하기 최종.[결과본]  (0) 2014.07.23
1. URL 경로 파일 다운로드해서 TXT형 저장하기  (0) 2014.07.23
[Java] 나머지 %  (0) 2014.06.01
Date  (0) 2014.02.01

/* URL로 M3U8파일 파싱해서 재생시간을 구하는 것이 필요했음. */



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 HttpDownloader {

    public static void main(String[]arg) {

        FileOutputStream fos = null;

        InputStream is = null;

        try {


// 다운로드할 URL 주소

String sourceUrl = "http://파일주소";  

// 생성될 TEXT 파일 경로,파일명

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


            fos = new FileOutputStream(""+ targetFilename);

            URL url = new URL(sourceUrl);

            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();

            }

        }

    }

}


'JAVA' 카테고리의 다른 글

3. 재생시간 구하기 최종.[결과본]  (0) 2014.07.23
2. 저장된 파일에서 필요한 값 찾기. readLine  (0) 2014.07.23
[Java] 나머지 %  (0) 2014.06.01
Date  (0) 2014.02.01
JCF(Java Collection Framework) : List / Map / Set  (0) 2014.01.18

 

double a = 69.67;
double b = 34;

 

System.out.println(a%b);
System.out.println(a-b*((int)(a/b)));

 


 

+ Recent posts