dimanche 8 avril 2018

How to determine if playlist is a Simple or Master version.

Hi I am working on creating a HLS project that will determine if a url is a playlist and return the values.

My code is below. Right now I get an error "Unknown File Format" because I am trying to get the computer to read what the file ends with. I would like to determine if the playlist is a simple or master playlist. I know their are tags at the end of each ".ts" and ".m3u8" but how do you get the computer to read the files tag?

package edu.psgv.sweng861;
import java.util.Scanner; 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;
import java.io.* ;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; 
/*
 * This demonstrates a java program that will read text from a URL 
 * @author Ahmeeya Goldman
 */


public class SimplePlaylist {
private static final Logger logger = LogManager.getLogger();

        /*
         * Main method is the entry point bringing together all the functions in this class
         */
        public static void main(String[] args) throws IOException{ 
                
                logger.info(">>main()");
                logger.debug(">>main()");
                logger.error(">>main()");
                AcceptInput();
                }
        
        /*
         * This method runs the program in interactive mode accepting user input 
         */
        private static void AcceptInput() {
                logger.info(">>AcceptInput()");
                // Create a new scanner object to read input 
                Scanner sc = new Scanner(System.in);
                //Show the version of this project 
                System.out.println("HLS Validator v. 2.0.0");
                //Creates variable to store user input 
                String input = null; 
                // Runs a loop to read input from command line
                do {
                        System.out.println("Please enter playlist url here(Enter 0 to Stop):");
                        //Reads the text the user entered in command line
                        input = sc.nextLine();
                        // Determines if the system should quit or show the content from URL 
                        if (!input.equalsIgnoreCase("0") && !input.isEmpty()) {
                                //System.out.println("Loading Files Please Stand By");
                                try {
                                        //Trys to read the files from the user input 
                                        String output = getPlaylistUrl(input);
                                        //Accepts user input 
                                        if (validateRecord(output) && validatePlaylist(output))
                                        System.out.println(output);
                                        //Declines user input 
                                        else {
                                                logger.info("User Entered Unknown File Type");
                                                logger.debug("User Entered Unknown File Type");
                                                logger.error("User Entered Unknown File Type");
                                                System.out.println("Unknown File Format");
                                                
                                        }
                                } catch (FileNotFoundException ex) { 
                                        System.out.println("File Not Found");
                                        logger.info("No Files Were Found In Path User Entered");
                                        logger.debug("An Error Was Found In The Users Input");
                                        logger.error("An Error Was Found In The Users Input");



                                //Attempts to catch wrong url paths
                                } catch (MalformedURLException ex) {
                                        System.out.println("Error In Url Format");
                                        logger.info("An Error Was Found In The Url The User Entered");
                                        logger.debug("An Error Was Found In The Users Input");
                                        logger.error("An Error Was Found In The Users Input");



                                //Attempts to catch an error from the user input
                                }catch (IOException ex) {
                                        System.out.println("404 Resource Not Found");
                                        logger.info("An Error Was Found In The Users Input");
                                        logger.debug("An Error Was Found In The Users Input");
                                        logger.error("An Error Was Found In The Users Input");



                                }
                        //Ends Program 
                        } else {
                                System.out.println("Program Ended Succesfully");
                                logger.info("The Program Was Ran And Ended Succesfully");
                                logger.debug("The Program Was Ran And Ended Succesfully");
                                logger.error("The Program Was Ran And Ended Succesfully");

                                System.exit(0);
                                sc.close();
                                
                        }
                } while (!input.equalsIgnoreCase("0"));
        }
        
        /*
         * getPlayListUrl() reads the contents of a text file and returns a long string containing the lines of the file.
         *  @param theUrl path to the input url.
         * @return string with all lines from file.
         */
        private static String getPlaylistUrl(String theUrl) throws 
        FileNotFoundException, MalformedURLException, IOException{
                String content = "";
                        //Creates a url variable 
                        URL url = new URL(theUrl);
                        //Cretes a urlConnection variable 
                        URLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        //Wraps the urlConnection in a BufferedReader 
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                        String line;
                        
                        while ((line = bufferedReader.readLine()) != null) {
                                content += line + "\n";
                        }
                        bufferedReader.close();
        
                
        
                return content;
                
        }
/*
 * Checks the first line of the read file to determine if it is .M3U8 File
 * @param record is the string variable containg all the lines read 
 * return true only if the record is valid 
 */
          private static boolean validateRecord(String record)
          {
                  return record.startsWith(("#EXT-X-STREAM-INF"));
          }
          
 /*
 *Checks to see if URL enetered begins with http 
 * @param pass the url 
 * return true if url valid 
*/
          
          private static boolean validateURL(String url)
          {
                  return url.startsWith(("http"));
          }
          
          private static boolean validatePlaylist(String playlist) {
                  return playlist.endsWith((".m3u8"));
          }
        
}

Aucun commentaire:

Enregistrer un commentaire