Ignore:
Timestamp:
04/14/07 15:52:46 (5 years ago)
Author:
jlee
Message:

Add code from David Grundberg's KuwaiiRiver?; preparation to combine DVD and SingleVideo? code; improvements to the shitty stuff (more modular conversion code)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/thestaticvoid/iriverter/MPlayerInfo.java

    r157 r161  
    22 * MPlayerInfo.java 
    33 * Copyright (C) 2005-2007 James Lee 
     4 * Copyright (C) 2007 David Grundberg 
    45 * 
    56 * This program is free software; you can redistribute it and/or 
     
    3435        private boolean commandFound = true; 
    3536         
    36         public MPlayerInfo(String video) throws MPlayerNotFoundException { 
    37                 this(video, MPlayerInfo.getMPlayerPath()); 
    38         } 
    39          
    40         public MPlayerInfo(String video, String mplayerPath) { 
    41                 this(video, null, mplayerPath); 
    42         } 
    43  
    44         public MPlayerInfo(String video, String dvdDrive, String mplayerPath) { 
    45                 String[] command = null; 
    46                 if (dvdDrive != null) 
    47                         command = new String[]{mplayerPath + File.separator + MPLAYER_BIN, "-vo", "null", "-ao", "null", "-frames", "1", "-dvd-device", dvdDrive, video.toString(), "-v", "-identify"}; 
    48                 else 
    49                         command = new String[]{mplayerPath + File.separator + MPLAYER_BIN, "-vo", "null", "-ao", "null", "-frames", "1", video.toString(), "-identify"}; 
     37        public MPlayerInfo(InputVideo inputVideo) throws MPlayerNotFoundException { 
     38                this(inputVideo, MPlayerInfo.getMPlayerPath()); 
     39        } 
     40 
     41        public MPlayerInfo(InputVideo inputVideo, String mplayerPath) { 
     42                String[] command = inputVideo.appendToCommand(new String[]{mplayerPath + File.separator + MPLAYER_BIN, "-v", "-identify", "-vo", "null", "-ao", "null", "-frames", "1"}); 
    5043                 
    5144                String commandStr = ""; 
     
    5346                        commandStr += command[i] + " "; 
    5447                Logger.logMessage(commandStr, Logger.INFO); 
     48 
     49                mplayerOutput = new StringBuffer(); 
    5550                 
    5651                try { 
     
    6055                } 
    6156                 
    62                 mplayerOutput = new StringBuffer(); 
     57                // We'll need to read the error stream too, otherwise mplayer may stall. 
     58                BlackHole.suck(proc.getErrorStream()); 
    6359                 
    6460                try { 
     
    7773         
    7874        public String getVideoFormat() { 
    79                 Matcher matcher = Pattern.compile("ID_VIDEO_FORMAT=.*").matcher(mplayerOutput); 
    80                 matcher.find(); 
    81                 String output = ""; 
    82                  
    83                 try { 
    84                         output = matcher.group(); 
    85                 } catch (Exception e) { 
    86                         // empty 
    87                 } 
    88  
    89                 return output.substring(output.indexOf('=') + 1); 
     75                Matcher matcher = Pattern.compile("ID_VIDEO_FORMAT=(.*)").matcher(mplayerOutput); 
     76 
     77                if (matcher.find()) 
     78                        return matcher.group(1); 
     79                 
     80                return ""; 
    9081        } 
    9182         
    9283        public int getLength() { 
    93                 Matcher matcher = Pattern.compile("ID_LENGTH=[0-9]*").matcher(mplayerOutput); 
    94                 matcher.find(); 
    95                 String output = ""; 
    96                  
    97                 try {    
    98                         output = matcher.group(); 
    99                 } catch (Exception e) { 
    100                         // empty 
    101                 } 
    102                  
    103                 try { 
    104                         return Integer.parseInt(output.substring(output.indexOf('=') + 1)); 
    105                 } catch (Exception e) { 
    106                         return 0; 
    107                 } 
     84                Matcher matcher = Pattern.compile("ID_LENGTH=([0-9]*)").matcher(mplayerOutput); 
     85         
     86                if (matcher.find()) 
     87                        return Integer.parseInt(matcher.group(1)); 
     88                 
     89                return 0; 
    10890        } 
    10991         
    11092        public int getNumberOfTitles() { 
    111                 Matcher matcher = Pattern.compile("ID_DVD_TITLES=[0-9]*").matcher(mplayerOutput); 
    112                 matcher.find(); 
    113                 String output = matcher.group(); 
    114          
    115                 return Integer.parseInt(output.substring(14)); 
     93                Matcher matcher = Pattern.compile("ID_DVD_TITLES=([0-9]*)").matcher(mplayerOutput); 
     94 
     95                if (matcher.find()) 
     96                        return Integer.parseInt(matcher.group(1)); 
     97                 
     98                return 0; 
    11699        } 
    117100         
     
    127110        public Map getAudioStreams() { 
    128111                Map languages = new LinkedHashMap(); 
    129                  
    130112                languages.put("Default", "-1"); 
    131                  
    132                 Matcher matcher = Pattern.compile("audio stream: [0-9]* audio format: ac3.*").matcher(mplayerOutput); 
    133                 while (matcher.find()) { 
    134                         String stream = matcher.group(); 
    135                         String lang = (Integer.parseInt(stream.substring(stream.indexOf("stream: ") + 8, stream.indexOf(" audio format:"))) + 1) + ". " + stream.substring(stream.indexOf("language: ") + 10, stream.indexOf(" aid:")); 
    136                         languages.put(lang, stream.substring(stream.indexOf(" aid: ") + 6)); 
    137                 } 
    138                  
     113 
     114                /* 
     115                 * = Pattern.compile("audio stream: [0-9]* audio format: 
     116                 * ac3.*").matcher(mplayerOutput); while (matcher.find()) { String 
     117                 * stream = matcher.group(); String lang = 
     118                 * (Integer.parseInt(stream.substring(stream.indexOf("stream: ") + 8, 
     119                 * stream.indexOf(" audio format:"))) + 1) + ". " + 
     120                 * stream.substring(stream.indexOf("language: ") + 10, stream.indexOf(" 
     121                 * aid:")); languages.put(lang, stream.substring(stream.indexOf(" aid: ") + 
     122                 * 6)); } 
     123                 */ 
     124 
     125                // Parse embedded audio streams (matroska) 
     126                /* 
     127                 * if (languages.size() == 1) { matcher = Pattern.compile(" -aid 
     128                 * ([0-9]{1,})[ ,-]* -alang ([a-z]{2,})").matcher(mplayerOutput); while 
     129                 * (matcher.find()) { String aid = matcher.group(1); String lang = 
     130                 * matcher.group(2); languages.put(lang, aid); } } 
     131                 */ 
     132 
     133                // Parse embedded audio streams (tested with matroska, ogg vorbis) 
     134                if (languages.size() == 1) { 
     135                        Matcher matcher = Pattern.compile("^ID_AUDIO_ID=([0-9]+)$",     Pattern.MULTILINE).matcher(mplayerOutput); 
     136                         
     137                        List audioIds = new ArrayList(); 
     138                        while (matcher.find()) 
     139                                audioIds.add(matcher.group(1)); 
     140                         
     141                        if (audioIds.size() > 1) 
     142                                for (int i = 0; i < audioIds.size(); i++) { 
     143                                        String aid = (String) audioIds.get(i); 
     144                                         
     145                                        // Find out audio stream language if available 
     146                                        // Default string in case there is no corresponding 
     147                                        // ID_AID_x_LANG (can happen) 
     148                                        String description = "Unknown"; 
     149                                        Matcher descMatcher = Pattern.compile("^ID_AID_" + aid + "_LANG=(.*)$", Pattern.MULTILINE).matcher(mplayerOutput); 
     150                                        if (descMatcher.find()) 
     151                                                description = descMatcher.group(1); 
     152 
     153                                        // Add format info if DVD (hack) 
     154                                        descMatcher = Pattern.compile("^audio stream: .* format: (.*) language: .* aid: " + aid + ".$", Pattern.MULTILINE).matcher(mplayerOutput); 
     155                                        if (descMatcher.find()) 
     156                                                description = description + " " + descMatcher.group(1); 
     157                                        description = aid + ": " + description; 
     158                                        // System.out.println(aid+":---"+lang); 
     159 
     160                                        languages.put(description, aid); 
     161                                } 
     162                } 
     163 
    139164                return languages; 
    140165        } 
     
    142167        public Map getSubtitleLanguages() { 
    143168                Map languages = new LinkedHashMap(); 
    144                  
    145169                languages.put("None", "-1"); 
    146170                 
    147                 Matcher matcher = Pattern.compile("[0-9]{1,} language: [a-z]{2}").matcher(mplayerOutput); 
    148                 while (matcher.find()) { 
    149                         String sub = matcher.group(); 
    150                         String lang = (Integer.parseInt(sub.substring(0, sub.indexOf(' '))) + 1) + ". " + sub.substring(sub.indexOf(": ") + 2); 
    151                         languages.put(lang, sub.substring(0, sub.indexOf(' '))); 
    152                 } 
    153                  
     171                /* 
     172                 * Matcher matcher = Pattern.compile("[0-9]{1,} language: 
     173                 * [a-z]{2}").matcher(mplayerOutput); while (matcher.find()) { String 
     174                 * sub = matcher.group(); String lang = 
     175                 * (Integer.parseInt(sub.substring(0, sub.indexOf(' '))) + 1) + ". " + 
     176                 * sub.substring(sub.indexOf(": ") + 2); languages.put(lang, 
     177                 * sub.substring(0, sub.indexOf(' '))); } 
     178                 */ 
     179 
     180                /* 
     181                 * // Parse embedded subtitles (matroska) if (languages.size() == 1) { 
     182                 * matcher = Pattern.compile(" -sid ([0-9]{1,})[ ,-]* -slang 
     183                 * ([a-z]{2,})").matcher(mplayerOutput); while (matcher.find()) { String 
     184                 * sid = matcher.group(1); String lang = matcher.group(2); 
     185                 * languages.put(lang, sid); } } 
     186                 */ 
     187                 
     188                // Parse embedded subtitles (tested with matroska, ogg vorbis) 
     189                if (languages.size() == 1) { 
     190                        Matcher matcher = Pattern.compile("^ID_SUBTITLE_ID=([0-9]+)$", Pattern.MULTILINE).matcher(mplayerOutput); 
     191                        while (matcher.find()) { 
     192                                String sid = matcher.group(1); 
     193                                String description = "Unknown"; 
     194 
     195                                Matcher descMatcher = Pattern.compile("^ID_SID_" + sid + "_LANG=(.*)$", Pattern.MULTILINE).matcher(mplayerOutput); 
     196                                if (descMatcher.find()) 
     197                                        description = descMatcher.group(1); 
     198 
     199                                // Add name if found (I've seen Matroska use these) Here's an 
     200                                // example: 
     201                                // ID_SUBTITLE_ID=0 
     202                                // ID_SID_0_NAME=Normal Subtitles 
     203                                // ID_SID_0_LANG=eng 
     204                                // [mkv] Track ID 4: subtitles (S_TEXT/SSA) "Normal Subtitles", 
     205                                // -sid 0, -slang eng 
     206                                // ID_SUBTITLE_ID=1 
     207                                // ID_SID_1_NAME=Subtitles with Karaoke 
     208                                // ID_SID_1_LANG=eng 
     209                                // [mkv] Track ID 5: subtitles (S_TEXT/SSA) "Subtitles with 
     210                                // Karaoke", -sid 1, -slang eng 
     211 
     212                                descMatcher = Pattern.compile("^ID_SID_" + sid + "_NAME=(.*)$", Pattern.MULTILINE).matcher(mplayerOutput); 
     213                                if (descMatcher.find()) 
     214                                        description = description + ": " + descMatcher.group(1); 
     215 
     216                                // hack: Add matroska subtitle format info. 
     217                                String t = "^\\[mkv\\] Track ID .*: subtitles \\((.*)\\).* -sid " + sid + ",.*$"; 
     218                                System.out.println("muh: " + t); 
     219                                descMatcher = Pattern.compile(t, Pattern.MULTILINE).matcher(mplayerOutput); 
     220                                if (descMatcher.find()) 
     221                                        description = description + " (" + descMatcher.group(1) + ")"; 
     222 
     223                                description = sid + ": " + description; 
     224                                // System.out.println(sid+":---"+descr); 
     225 
     226                                languages.put(description, sid); 
     227                        } 
     228                } 
     229 
    154230                return languages; 
    155231        } 
    156232 
    157233        public double getFrameRate() { 
    158                 double frameRate = 0; 
    159                 Matcher matcher = Pattern.compile("[0-9.]* fps").matcher(mplayerOutput); 
     234                Matcher matcher = Pattern.compile("([0-9.]+) fps").matcher(mplayerOutput); 
    160235 
    161236                if (!matcher.find()) 
    162237                        return -1; 
    163238                 
    164                 frameRate = Double.parseDouble(matcher.group().substring(0, 
    165                                 matcher.group().indexOf(' '))); 
    166  
    167                 return frameRate; 
     239                return Double.parseDouble(matcher.group(1)); 
    168240        } 
    169241 
    170242        public Dimensions getDimensions() { 
    171                 Matcher matcher = Pattern.compile("=> [0-9]*x[0-9]*").matcher(mplayerOutput); 
     243                Matcher matcher = Pattern.compile("ID_VIDEO_WIDTH=(\\d+)").matcher(mplayerOutput); 
     244 
     245                if (matcher.find()) { 
     246                        int width = Integer.parseInt(matcher.group(1)); 
     247                        matcher = Pattern.compile("ID_VIDEO_HEIGHT=(\\d+)").matcher(mplayerOutput); 
     248                        if (matcher.find()) { 
     249                                int height = Integer.parseInt(matcher.group(1)); 
     250                                matcher = Pattern.compile("Movie-Aspect is (\\d+(?:\\.\\d+)?):1 - prescaling to correct movie aspect.").matcher(mplayerOutput); 
     251                                if (matcher.find()) { 
     252                                        double scale = Double.parseDouble(matcher.group(1)); 
     253                                        width = (int) Math.round(height * scale); 
     254                                } 
     255                                return new Dimensions(width, height); 
     256                        } 
     257                } 
     258 
     259                matcher = Pattern.compile("=> [0-9]*x[0-9]*").matcher(mplayerOutput); 
    172260 
    173261                if (!matcher.find()) 
    174262                        return new Dimensions(-1, -1); 
    175  
    176                 return new Dimensions(matcher.group().substring(matcher.group().indexOf(' ') + 1)); 
     263                 
     264                matcher = Pattern.compile("=> ([0-9]*x[0-9]*)").matcher(mplayerOutput); 
     265 
     266                if (!matcher.find()) 
     267                        return new Dimensions(-1, -1); 
     268 
     269                return new Dimensions(matcher.group(1)); 
    177270        } 
    178271         
Note: See TracChangeset for help on using the changeset viewer.