Changeset 157


Ignore:
Timestamp:
04/12/07 17:06:18 (5 years ago)
Author:
jlee
Message:

iriverter: Now with less god class! Only SingleVideo? transitioned. No error detection yet

Location:
trunk
Files:
2 added
11 deleted
5 edited

Legend:

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

    r153 r157  
    1 /* 
    2  * Converter.java 
    3  * Copyright (C) 2005-2007 James Lee 
    4  * 
    5  * This program is free software; you can redistribute it and/or 
    6  * modify it under the terms of the GNU General Public License 
    7  * as published by the Free Software Foundation; either version 2 
    8  * of the License, or (at your option) any later version. 
    9  *  
    10  * This program is distributed in the hope that it will be useful, 
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    13  * GNU General Public License for more details. 
    14  *  
    15  * You should have received a copy of the GNU General Public License 
    16  * along with this program; if not, write to the Free Software 
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
    18  * 02110-1301, USA. 
    19  *  
    20  * $Id$ 
    21  */ 
    221package org.thestaticvoid.iriverter; 
    232 
    24 import java.io.*; 
    25 import java.util.*; 
    26  
    273public class Converter extends Thread { 
    28         private List jobs, notSplitVideos; 
     4        private Job[] jobs; 
    295        private ProgressDialogInfo progressDialogInfo; 
    30         private String mplayerPath; 
    31         private Process proc; 
    32         private boolean isCanceled; 
    33         private int exitCode; 
     6        private MencoderCommand currentCommand; 
    347         
    35         public Converter(List jobs, ProgressDialogInfo progressDialogInfo, String mplayerPath) { 
    36                 this.jobs = Converter.checkForOverwritingFiles(Converter.expandSingleJobsToMultiple(Converter.removeInvalidJobs(jobs))); 
     8        public Converter(Job[] jobs, ProgressDialogInfo progressDialogInfo) { 
     9                this.jobs = jobs; 
    3710                this.progressDialogInfo = progressDialogInfo; 
    38                 this.mplayerPath = mplayerPath; 
    39                 isCanceled = false; 
    40                  
    41                 notSplitVideos = new ArrayList(); 
    42                  
    43                 progressDialogInfo.setTotalJobs(this.jobs.size()); 
    44         } 
    45          
    46         public static List removeInvalidJobs(List jobs) { 
    47                 List newJobs = new ArrayList(); 
    48                  
    49                 for (int i = 0; i < jobs.size(); i++) { 
    50                         boolean validOutput = true; 
    51                         if (jobs.get(i) instanceof OutputVideoInfo) { 
    52                                 OutputVideoInfo outputVideoInfo = (OutputVideoInfo) jobs.get(i); 
    53                                  
    54                                 if (outputVideoInfo.getOutputVideo().equals("")) 
    55                                         continue; 
    56                                  
    57                                 if (!outputVideoInfo.getOutputVideo().endsWith("." + ConverterOptions.getCurrentProfile().getWrapperFormat()) && !outputVideoInfo.getOutputVideo().equals("")) 
    58                                         outputVideoInfo.setOutputVideo(outputVideoInfo.getOutputVideo() + "." + ConverterOptions.getCurrentProfile().getWrapperFormat()); 
    59                                  
    60                                 if (!new File(outputVideoInfo.getOutputVideo()).getParentFile().exists()) 
    61                                         validOutput = new File(outputVideoInfo.getOutputVideo()).getParentFile().mkdirs(); 
    62                                  
    63                                 validOutput = validOutput && new File(outputVideoInfo.getOutputVideo()).getParentFile().canWrite(); 
    64                         } 
    65                          
    66                         if (jobs.get(i) instanceof SingleVideoInfo) { 
    67                                 SingleVideoInfo singleVideoInfo = (SingleVideoInfo) jobs.get(i); 
    68                                 if (new File(singleVideoInfo.getInputVideo()).exists() && validOutput) 
    69                                         newJobs.add(singleVideoInfo); 
    70                         } else if (jobs.get(i) instanceof DirectoryInfo) { 
    71                                 DirectoryInfo directoryInfo = (DirectoryInfo) jobs.get(i); 
    72                                 if (new File(directoryInfo.getInputDirectory()).exists() && !directoryInfo.getOutputDirectory().equals("")) 
    73                                         if (!new File(directoryInfo.getOutputDirectory()).exists()) 
    74                                                 validOutput = new File(directoryInfo.getOutputDirectory()).mkdirs(); 
    75                                  
    76                                         if (validOutput) 
    77                                                 newJobs.add(directoryInfo); 
    78                         } else if (jobs.get(i) instanceof DVDInfo) { 
    79                                 DVDInfo dvdInfo = (DVDInfo) jobs.get(i); 
    80                                 if (!dvdInfo.getDrive().equals("") && validOutput) 
    81                                         newJobs.add(dvdInfo); 
    82                         } else if (jobs.get(i) instanceof ManualSplitInfo) { 
    83                                 ManualSplitInfo manualSplitInfo = (ManualSplitInfo) jobs.get(i); 
    84                                 if (!manualSplitInfo.getVideo().equals("") && manualSplitInfo.getMarks().length > 2) 
    85                                         newJobs.add(manualSplitInfo); 
    86                         } else if (jobs.get(i) instanceof JoinVideosInfo) { 
    87                                 JoinVideosInfo joinVideosInfo = (JoinVideosInfo) jobs.get(i); 
    88                                 if (joinVideosInfo.getInputVideos().length > 0 && validOutput) 
    89                                         newJobs.add(joinVideosInfo); 
    90                         } 
    91                 } 
    92                          
    93                 return newJobs; 
    94         } 
    95          
    96         public static List expandSingleJobsToMultiple(List jobs) { 
    97                 List newJobs = new ArrayList(); 
    98                  
    99                 for (int i = 0; i < jobs.size(); i++)    
    100                         if (jobs.get(i) instanceof DirectoryInfo) 
    101                                 newJobs.addAll(convertDirectoryToSingleVideos((DirectoryInfo) jobs.get(i))); 
    102                         else if (jobs.get(i) instanceof DVDInfo) 
    103                                 newJobs.addAll(separateDVDChaptersToSingleDVDJobs((DVDInfo) jobs.get(i))); 
    104                         else if (jobs.get(i) instanceof ManualSplitInfo) 
    105                                 newJobs.addAll(separateMultipleSplitJobsToOneSplitJob((ManualSplitInfo) jobs.get(i))); 
    106                         else 
    107                                 newJobs.add(jobs.get(i)); 
    108                  
    109                 return newJobs; 
    110         } 
    111          
    112         public static List convertDirectoryToSingleVideos(DirectoryInfo directoryInfo) { 
    113                 List newJobs = new ArrayList(); 
    114                  
    115                 String[] directory = new File(directoryInfo.getInputDirectory()).list(new VideoFileFilter()); 
    116                  
    117                 for (int i = 0; i < directory.length; i++) 
    118                         if (new File(directoryInfo.getInputDirectory() + File.separator + directory[i]).isDirectory() && directoryInfo.getConvertSubdirectories()) 
    119                                 newJobs.addAll(convertDirectoryToSingleVideos(new DirectoryAdapter(directoryInfo.getInputDirectory() + File.separator + directory[i], directoryInfo.getOutputDirectory() + File.separator + directory[i], directoryInfo.getConvertSubdirectories()))); 
    120                         else if (new File(directoryInfo.getInputDirectory() + File.separator + directory[i]).isFile()) 
    121                                 newJobs.add(new SingleVideoAdapter(directoryInfo.getInputDirectory() + File.separator + directory[i], directoryInfo.getOutputDirectory() + File.separator + directory[i].substring(0, directory[i].lastIndexOf('.')) + "." + ConverterOptions.getCurrentProfile().getProfileName() + ".avi")); 
    122                          
    123                 return newJobs; 
    124         } 
    125          
    126         public static List separateDVDChaptersToSingleDVDJobs(DVDInfo dvdInfo) { 
    127                 List newJobs = new ArrayList(); 
    128          
    129                 Chapters[] chapters = dvdInfo.getChapters(); 
    130                  
    131                 if (chapters == null) 
    132                         newJobs.add(dvdInfo); 
    133                 else 
    134                         for (int i = 0; i < chapters.length; i++) { 
    135                                 String outputVideo = ""; 
    136                                 if (chapters[i].getFirstChapter() == chapters[i].getLastChapter()) 
    137                                         outputVideo = dvdInfo.getOutputVideo().substring(0, dvdInfo.getOutputVideo().lastIndexOf('.')) + ".ch" + chapters[i].getFirstChapterPadded() + ".avi"; 
    138                                 else 
    139                                         outputVideo = dvdInfo.getOutputVideo().substring(0, dvdInfo.getOutputVideo().lastIndexOf('.')) + ".ch" + chapters[i].getFirstChapterPadded() + "-" + chapters[i].getLastChapterPadded() + ".avi"; 
    140                                  
    141                                 newJobs.add(new DVDAdapter(dvdInfo.getDrive(), dvdInfo.getTitle(), new Chapters[]{chapters[i]}, dvdInfo.getAudioStream(), dvdInfo.getSubtitles(), outputVideo)); 
    142                         } 
    143                  
    144                 return newJobs; 
    145         } 
    146          
    147         public static List separateMultipleSplitJobsToOneSplitJob(ManualSplitInfo manualSplitInfo) { 
    148                 List newJobs = new ArrayList(); 
    149                  
    150                 for (int i = 0; (i + 1) < manualSplitInfo.getMarks().length; i++) 
    151                         newJobs.add(new ManualSplitAdapter(manualSplitInfo.getVideo(), new Mark[]{manualSplitInfo.getMarks()[i], manualSplitInfo.getMarks()[i + 1]}, i + 1)); 
    152                  
    153                 return newJobs;          
    154         } 
    155          
    156         public static List checkForOverwritingFiles(List jobs) { 
    157                 List newJobs = new ArrayList(); 
    158                  
    159                 for (int i = 0; i < jobs.size(); i++) { 
    160                         if (!(jobs.get(i) instanceof OutputVideoInfo)) 
    161                                 newJobs.add(jobs.get(i)); 
    162                         else if (new File(((OutputVideoInfo) jobs.get(i)).getOutputVideo()).exists()) { 
    163                                 if (OverwriteDialog.overwriteFile(((OutputVideoInfo) jobs.get(i)).getOutputVideo())) 
    164                                         newJobs.add(jobs.get(i)); 
    165                         } else 
    166                                 newJobs.add(jobs.get(i)); 
    167                 } 
    168                  
    169                 return newJobs; 
    17011        } 
    17112         
    17213        public void run() { 
    173                 for (int i = 0; i < jobs.size() && !isCanceled; i++) { 
    174                         progressDialogInfo.setCurrentJob(i + 1); 
    175                          
    176                         if (jobs.get(i) instanceof SingleVideoInfo) 
    177                                 convertSingleVideo((SingleVideoInfo) jobs.get(i));; 
    178                         if (jobs.get(i) instanceof DVDInfo) 
    179                                 convertDVD((DVDInfo) jobs.get(i)); 
    180                         if (jobs.get(i) instanceof ManualSplitInfo) 
    181                                 manuallySplitVideo((ManualSplitInfo) jobs.get(i)); 
    182                         if (jobs.get(i) instanceof JoinVideosInfo) 
    183                                 joinVideos((JoinVideosInfo) jobs.get(i)); 
     14                for (int i = 0; i < jobs.length; i++) { 
     15                        MencoderCommand[] mencoderCommands = jobs[i].getMencoderCommands(); 
     16                        for (int j = 0; j < mencoderCommands.length; j++) { 
     17                                currentCommand = mencoderCommands[j]; 
     18                                currentCommand.run(progressDialogInfo); 
     19                        } 
    18420                } 
    185                  
    186                 if (!isCanceled) 
    187                         progressDialogInfo.complete(exitCode == 0); 
    18821        } 
    18922         
    19023        public void cancel() { 
    191                 isCanceled = true; 
    192                  
    193                 if (proc != null) 
    194                         proc.destroy(); 
    195         } 
    196          
    197         private List prepareBaseCommandList(String inputVideo, String outputVideo, MPlayerInfo info) {           
    198                 List commandList = new ArrayList(); 
    199                  
    200                 commandList.add(mplayerPath + MPlayerInfo.MENCODER_BIN); 
    201                  
    202                 commandList.add(inputVideo); 
    203                 commandList.add("-o"); 
    204                 commandList.add(outputVideo); 
    205                  
    206                 if (ConverterOptions.getCurrentProfile().getWrapperFormat().equals("mp4")) { 
    207                         commandList.add("-of"); 
    208                         commandList.add("lavf"); 
    209                         commandList.add("-lavfopts"); 
    210                         commandList.add("format=mp4:i_certify_that_my_video_stream_does_not_use_b_frames"); 
    211                 } 
    212                  
    213                 commandList.add("-ovc"); 
    214                 if (ConverterOptions.getCurrentProfile().getVideoFormat().equals("h264")) { 
    215                         commandList.add("x264"); 
    216                         commandList.add("-x264encopts"); 
    217                         commandList.add("bitrate=" + ConverterOptions.getVideoBitrate() + ":bframes=0:level_idc=13:nocabac"); 
    218                 } else { 
    219                         commandList.add("xvid"); 
    220                         commandList.add("-xvidencopts"); 
    221                         commandList.add("bitrate=" + ConverterOptions.getVideoBitrate() + ":max_bframes=0"); 
    222                 } 
    223                  
    224                 commandList.add("-oac"); 
    225                 if (ConverterOptions.getCurrentProfile().getAudioFormat().equals("aac")) { 
    226                         commandList.add("faac"); 
    227                         commandList.add("-faacopts"); 
    228                         commandList.add("br=" + ConverterOptions.getAudioBitrate() + ":object=1"); 
    229                 } else { 
    230                         commandList.add("mp3lame"); 
    231                         commandList.add("-lameopts"); 
    232                         commandList.add("mode=0:cbr:br=" + ConverterOptions.getAudioBitrate()); 
    233                 } 
    234                  
    235                 double ofps = (info.getFrameRate() > ConverterOptions.getCurrentProfile().getMaxFrameRate() ? ConverterOptions.getCurrentProfile().getMaxFrameRate() : info.getFrameRate()); 
    236                 if (info.getFrameRate() != ofps && info.getFrameRate() < 1000) {        // HACK: wmv always shows 1000 fps 
    237                         commandList.add("-vf-add"); 
    238                         commandList.add("filmdint=io=" + ((int) Math.round(info.getFrameRate() * 1000)) + ":" + ((int) Math.round(ofps * 1000))); 
    239                 } 
    240                  
    241                 int scaledWidth = ConverterOptions.getDimensions().getWidth(); 
    242                 int scaledHeight = (info.getDimensions().getHeight() * ConverterOptions.getDimensions().getWidth()) / info.getDimensions().getWidth(); 
    243                  
    244                 if (scaledHeight > ConverterOptions.getDimensions().getHeight()) { 
    245                         scaledWidth = (scaledWidth * ConverterOptions.getDimensions().getHeight()) / scaledHeight; 
    246                         scaledHeight = ConverterOptions.getDimensions().getHeight(); 
    247                 } 
    248                  
    249                 commandList.add("-vf-add"); 
    250                 if (ConverterOptions.getPanAndScan()) 
    251                         commandList.add("scale=" + ((int) ((info.getDimensions().getWidth()) * (((double) ConverterOptions.getDimensions().getHeight()) / (double) info.getDimensions().getHeight()))) + ":" + ConverterOptions.getDimensions().getHeight() + ",crop=" + ConverterOptions.getDimensions().getWidth() + ":" + ConverterOptions.getDimensions().getHeight()); 
    252                 else 
    253                         commandList.add("scale=" + scaledWidth + ":" + scaledHeight + ",expand=" + ConverterOptions.getDimensions().getWidth() + ":" + ConverterOptions.getDimensions().getHeight()); 
    254                  
    255                 commandList.add("-vf-add"); 
    256                 commandList.add("harddup"); 
    257                  
    258                 if (ConverterOptions.getVolumeFilter().equals(VolumeFilter.VOLNORM)) { 
    259                         commandList.add("-af"); 
    260                         commandList.add("volnorm"); 
    261                 } else if (ConverterOptions.getVolumeFilter().equals(VolumeFilter.VOLUME)) { 
    262                         commandList.add("-af"); 
    263                         commandList.add("volume=" + ConverterOptions.getGain()); 
    264                 } 
    265                  
    266                 commandList.add("-ofps"); 
    267                 commandList.add("" + ofps); 
    268                 commandList.add("-srate"); 
    269                 commandList.add("44100"); 
    270                  
    271                 if (!ConverterOptions.getAutoSync()) { 
    272                         commandList.add("-delay"); 
    273                         commandList.add("" + (ConverterOptions.getAudioDelay() / 1000.0)); 
    274                 } 
    275                  
    276                 return commandList; 
    277         } 
    278          
    279         public void convertSingleVideo(SingleVideoInfo singleVideoInfo) { 
    280                 Logger.logMessage("Single Video: " + singleVideoInfo.getInputVideo(), Logger.INFO); 
    281                  
    282                 progressDialogInfo.setInputVideo(new File(singleVideoInfo.getInputVideo()).getName()); 
    283                 progressDialogInfo.setOutputVideo(new File(singleVideoInfo.getOutputVideo()).getName()); 
    284                 progressDialogInfo.setStatus("Gathering information about the input video..."); 
    285                  
    286                 MPlayerInfo info = new MPlayerInfo(singleVideoInfo.getInputVideo(), mplayerPath); 
    287                 if (!info.videoSupported()) { 
    288                         Logger.logMessage("Unsupported video", Logger.ERROR); 
    289                         return; 
    290                 } 
    291                  
    292                 List commandList = prepareBaseCommandList(singleVideoInfo.getInputVideo(), singleVideoInfo.getOutputVideo(), info); 
    293                  
    294                 String[] command = new String[commandList.size()]; 
    295                 for (int i = 0; i < command.length; i++) 
    296                         command[i] = (String) commandList.get(i); 
    297                  
    298                 if (!isCanceled) { 
    299                         new File(singleVideoInfo.getOutputVideo()).getParentFile().mkdirs(); 
    300                         progressDialogInfo.setStatus("Converting"); 
    301                         splitVideo(singleVideoInfo.getOutputVideo(), runConversionCommand(command)); 
    302                 } 
    303         } 
    304          
    305         public void convertDVD(DVDInfo dvdInfo) {                
    306                 String inputVideo = "Title " + dvdInfo.getTitle() + " of the DVD at " + dvdInfo.getDrive(); 
    307                  
    308                 Chapters[] chapters = dvdInfo.getChapters(); 
    309                 if (chapters != null) { 
    310                         if (chapters[0].getFirstChapter() == chapters[0].getLastChapter()) 
    311                                 inputVideo = "Chapter " + chapters[0].getFirstChapter() + " of " + inputVideo; 
    312                         else  
    313                                 inputVideo = "Chapters " + chapters[0].getFirstChapter() + "-" + chapters[0].getLastChapter() + " of " + inputVideo; 
    314                 } 
    315                  
    316                 Logger.logMessage("DVD: " + inputVideo, Logger.INFO); 
    317                  
    318                 progressDialogInfo.setInputVideo(inputVideo); 
    319                 progressDialogInfo.setOutputVideo(new File(dvdInfo.getOutputVideo()).getName()); 
    320                 progressDialogInfo.setStatus("Gathering information about the input video..."); 
    321                  
    322                 MPlayerInfo info = new MPlayerInfo("dvd://" + dvdInfo.getTitle(), dvdInfo.getDrive(), mplayerPath); 
    323                  
    324                 List commandList = prepareBaseCommandList("dvd://" + dvdInfo.getTitle(), dvdInfo.getOutputVideo(), info); 
    325                  
    326                 commandList.add("-dvd-device"); 
    327                 commandList.add(dvdInfo.getDrive()); 
    328          
    329                 if (dvdInfo.getAudioStream() > -1) { 
    330                         commandList.add("-aid"); 
    331                         commandList.add("" + dvdInfo.getAudioStream()); 
    332                 } 
    333                  
    334                 if (dvdInfo.getSubtitles() > -1) { 
    335                         commandList.add("-sid"); 
    336                         commandList.add("" + dvdInfo.getSubtitles()); 
    337                 } 
    338                  
    339                 if (dvdInfo.getChapters() != null) { 
    340                         commandList.add("-chapter"); 
    341                         commandList.add(dvdInfo.getChapters()[0].getFirstChapter() + "-" + dvdInfo.getChapters()[0].getLastChapter()); 
    342                 } 
    343                  
    344                 String[] command = new String[commandList.size()]; 
    345                 for (int i = 0; i < command.length; i++) 
    346                         command[i] = (String) commandList.get(i); 
    347                  
    348                 if (!isCanceled) { 
    349                         new File(dvdInfo.getOutputVideo()).getParentFile().mkdirs(); 
    350                         progressDialogInfo.setStatus("Converting"); 
    351                         splitVideo(dvdInfo.getOutputVideo(), runConversionCommand(command)); 
    352                 } 
    353         } 
    354          
    355         public void manuallySplitVideo(ManualSplitInfo manualSplitInfo) { 
    356                 Logger.logMessage("Manual Split: " + manualSplitInfo.getVideo(), Logger.INFO); 
    357                  
    358                 String outputVideo = manualSplitInfo.getVideo().substring(0, manualSplitInfo.getVideo().lastIndexOf('.')) + ".part" + manualSplitInfo.getPart() + ".avi"; 
    359                  
    360                 progressDialogInfo.setInputVideo(manualSplitInfo.getVideo()); 
    361                 progressDialogInfo.setOutputVideo(outputVideo); 
    362                 progressDialogInfo.setStatus("Splitting"); 
    363                  
    364                 if (manualSplitInfo.getMarks()[0].getTime() == Mark.START_MARK) 
    365                         runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, manualSplitInfo.getVideo(), "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-endpos", "" + manualSplitInfo.getMarks()[1].getTime()}); 
    366                 else if (manualSplitInfo.getMarks()[1].getTime() == Mark.END_MARK) 
    367                         runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, manualSplitInfo.getVideo(), "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-ss", "" + manualSplitInfo.getMarks()[0].getTime()}); 
    368                 else 
    369                         runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, manualSplitInfo.getVideo(), "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-ss", "" + manualSplitInfo.getMarks()[0].getTime(), "-endpos", "" + (manualSplitInfo.getMarks()[1].getTime() - manualSplitInfo.getMarks()[0].getTime())}); 
    370         } 
    371          
    372         public void joinVideos(JoinVideosInfo joinVideosInfo) { 
    373                 try { 
    374                         String[] inputVideos = joinVideosInfo.getInputVideos(); 
    375                         /* String[] tempVideos = new String[inputVideos.length]; 
    376                          
    377                         for (int i = 0; i < inputVideos.length; i++) { 
    378                                 File tempFile = File.createTempFile("iriverter-", ".avi"); 
    379                                 tempFile.deleteOnExit(); 
    380                                  
    381                                 progressDialogInfo.setInputVideo(new File(inputVideos[i]).getName()); 
    382                                 progressDialogInfo.setOutputVideo(tempFile.getName()); 
    383                                 progressDialogInfo.setStatus("Fixing header"); 
    384                                  
    385                                 runConversionCommand(new String[]{MPlayerInfo.getMPlayerPath() + "mencoder", "-idx", inputVideos[i], "-o", tempFile.toString(), "-ovc", "copy", "-oac", "copy"}); 
    386                                  
    387                                 tempVideos[i] = tempFile.toString(); 
    388                         } */ 
    389                          
    390                         File tempFile = File.createTempFile("iriverter-", ".avi"); 
    391                         tempFile.deleteOnExit(); 
    392                          
    393                         Logger.logMessage("Join Videos: " + tempFile, Logger.INFO); 
    394                          
    395                         progressDialogInfo.setOutputVideo(tempFile.getName()); 
    396                         progressDialogInfo.setStatus("Concatenating videos to a temporary file..."); 
    397                          
    398                         FileOutputStream out = new FileOutputStream(tempFile); 
    399                         // SequenceInputStream in = new SequenceInputStream(new ListOfFiles(tempVideos, progressDialogInfo)); 
    400                         SequenceInputStream in = new SequenceInputStream(new ListOfFiles(inputVideos, progressDialogInfo)); 
    401                         byte[] bytes = new byte[4096]; 
    402                         int length; 
    403                          
    404                         while ((length = in.read(bytes)) != -1 && !isCanceled) 
    405                                 out.write(bytes, 0, length); 
    406                          
    407                         progressDialogInfo.setPercentComplete(100); 
    408                          
    409                         if (!isCanceled) { 
    410                                 Logger.logMessage("Writing header...", Logger.INFO); 
    411                                  
    412                                 progressDialogInfo.setInputVideo(tempFile.getName()); 
    413                                 progressDialogInfo.setOutputVideo(new File(joinVideosInfo.getOutputVideo()).getName()); 
    414                                 progressDialogInfo.setStatus("Writing header"); 
    415                                 splitVideo(joinVideosInfo.getOutputVideo(), runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, "-forceidx", tempFile.toString(), "-o", joinVideosInfo.getOutputVideo(), "-ovc", "copy", "-oac", "copy"})); 
    416                         } 
    417                 } catch (IOException e) { 
    418                         // empty 
    419                 } 
    420         } 
    421          
    422         public int runConversionCommand(String[] command) { 
    423                 MencoderStreamParser inputStream = null; 
    424                 MencoderStreamParser errorStream = null; 
    425                  
    426                 String commandStr = ""; 
    427                 for (int i = 0; i < command.length; i++) 
    428                         commandStr += command[i] + " "; 
    429                 Logger.logMessage(commandStr, Logger.INFO); 
    430                  
    431                 try { 
    432                         proc = Runtime.getRuntime().exec(command); 
    433                          
    434                         inputStream = new MencoderStreamParser(progressDialogInfo); 
    435                         inputStream.parse(new BufferedReader(new InputStreamReader(proc.getInputStream()))); 
    436                         errorStream = new MencoderStreamParser(progressDialogInfo); 
    437                         errorStream.parse(new BufferedReader(new InputStreamReader(proc.getErrorStream()))); 
    438                          
    439                         exitCode = proc.waitFor(); 
    440                         proc = null; 
    441                 } catch (Exception e) { 
    442                         e.printStackTrace(); 
    443                 } 
    444                  
    445                 if (exitCode != 0) 
    446                         isCanceled = true; 
    447                  
    448                 return isCanceled ? 0 : ((errorStream.getLength() > -1) ? errorStream.getLength() : inputStream.getLength()); 
    449         } 
    450          
    451         public void splitVideo(String inputVideo, int length) { 
    452                 if (length < ConverterOptions.getSplitTime() * 60) 
    453                         return; 
    454                  
    455                 if (!ConverterOptions.getAutoSplit()) { 
    456                         notSplitVideos.add(inputVideo); 
    457                         return; 
    458                 } 
    459                  
    460                 int pieces = (length / (ConverterOptions.getSplitTime() * 60)) + 1; 
    461                 for (int i = 0; i < pieces; i++) { 
    462                         String outputVideo = inputVideo.substring(0, inputVideo.lastIndexOf('.')) + ".part" + (i + 1) + ".avi"; 
    463                          
    464                         progressDialogInfo.setInputVideo(new File(inputVideo).getName()); 
    465                         progressDialogInfo.setOutputVideo(new File(outputVideo).getName()); 
    466                         progressDialogInfo.setStatus("Splitting"); 
    467                          
    468                         if ((i + 1) == 1) 
    469                                 runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, inputVideo, "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-endpos", "" + (length / pieces)}); 
    470                         else if ((i + 1) == pieces) 
    471                                 runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, inputVideo, "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-ss", "" + (length / pieces) * i}); 
    472                         else 
    473                                 runConversionCommand(new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, inputVideo, "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-ss", "" + (length / pieces) * i, "-endpos", "" + (length / pieces)}); 
    474                 } 
    475         } 
    476          
    477         public List getNotSplitVideos() { 
    478                 return notSplitVideos; 
     24                currentCommand.cancel(); 
    47925        } 
    48026} 
  • trunk/src/org/thestaticvoid/iriverter/ConverterUI.java

    r153 r157  
    392392                 
    393393                if (e.getSource() == convert || e.getSource() == convertTool) { 
    394                         java.util.List jobs = new ArrayList(); 
     394                        Job[] jobs = new Job[tabFolder.getItemCount()]; 
    395395                        for (int i = 0; i < tabFolder.getItemCount(); i++) 
    396                                 jobs.add(tabFolder.getItem(i).getControl()); 
     396                                jobs[i] = (Job) tabFolder.getItem(i).getControl(); 
    397397                         
    398398                        progressDialog = new ProgressDialog(shell, SWT.NONE); 
    399399                         
    400                         boolean canceled = false; 
    401                         while (!canceled) 
    402                                 try { 
    403                                         Converter converter = new Converter(jobs, progressDialog, MPlayerInfo.getMPlayerPath()); 
    404                                         converter.start(); 
    405                                         progressDialog.open(); 
    406                                         converter.cancel(); 
    407                                          
    408                                         canceled = true; 
    409                                 } catch (MPlayerNotFoundException mpe) { 
    410                                         canceled = new MPlayerPathDialog(shell).open(); 
    411                                 } 
     400                        Converter converter = new Converter(jobs, progressDialog); 
     401                        converter.start(); 
     402                        progressDialog.open(); 
     403                        converter.cancel(); 
    412404                } 
    413405                 
     
    434426                } 
    435427                 
    436                 if (e.getSource() == newSingleVideo || e.getSource() == newSingleVideoTool) 
    437                         newSingleVideo(); 
    438                  
    439                 if (e.getSource() == newDirectory || e.getSource() == newDirectoryTool) 
    440                         newDirectory(); 
     428                if (e.getSource() == newSingleVideo || e.getSource() == newSingleVideoTool) { 
     429                        FileDialog fileDialog = new FileDialog(shell, SWT.OPEN); 
     430                        fileDialog.setText("Input Video"); 
     431                        fileDialog.setFilterExtensions(new String[]{"*.avi;*.vob;*.mkv;*.mpg;*.mpeg;*.mp4;*.ogm;*.mov;*.rm;*.ram;*.wmv;*.asf", "*.avi", "*.vob", "*.mkv", "*.mpg;*.mpeg;*.mp4", "*.ogm", "*.mov", "*.rm;*.ram", "*.wmv;*.asf", "*"}); 
     432                        fileDialog.setFilterNames(new String[]{"All Video Files", "AVI Video (*.avi)", "DVD Video Object (*.vob)", "Matroska Video (*.mkv)", "MPEG Video (*.mpg, *.mpeg, *.mp4)", "Ogg Video (*.ogm)", "Quicktime Movie (*.mov)", "Real Video (*.rm, *.ram)", "Windows Media Video (*.wmv, *.asf)", "All Files"}); 
     433                        String file = fileDialog.open(); 
     434                        if (file != null) 
     435                                newSingleVideo(file); 
     436                } 
     437                 
     438                /*if (e.getSource() == newDirectory || e.getSource() == newDirectoryTool) 
     439                        newDirectory();*/ 
    441440                 
    442441                if (e.getSource() == newDVD || e.getSource() == newDVDTool) 
     
    663662                                 
    664663                                if (file.isFile() && new VideoFileFilter().accept(file)) 
    665                                         newSingleVideo().setInputVideo(files[i]); 
     664                                        newSingleVideo(files[i].toString()); 
    666665                                else if (file.isDirectory()) 
    667666                                        if (new File(files[i] + File.separator + "VIDEO_TS").exists()) 
    668667                                                newDVD().setDrive(files[i]); 
    669                                         else 
    670                                                 newDirectory().setInputDirectory(files[i]); 
     668                                        /* else 
     669                                                newDirectory().setInputDirectory(files[i]);*/ 
    671670                        } 
    672671                }                
     
    677676        } 
    678677         
    679         private SingleVideo newSingleVideo() { 
     678        private SingleVideo newSingleVideo(String video) { 
    680679                CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE); 
    681                 SingleVideo singleVideo = new SingleVideo(tabFolder, SWT.NONE, tabItem); 
    682                 tabItem.setControl(singleVideo); 
    683                 tabFolder.setSelection(tabItem); 
    684                 tabChanged(false); 
     680                SingleVideo singleVideo = null; 
     681                 
     682                boolean canceled = false; 
     683                while (!canceled) 
     684                        try { 
     685                                singleVideo = new SingleVideo(tabFolder, SWT.NONE, tabItem, video, MPlayerInfo.getMPlayerPath());  
     686                                tabItem.setControl(singleVideo); 
     687                                tabFolder.setSelection(tabItem); 
     688                                tabChanged(false); 
     689                                canceled = true; 
     690                        } catch (MPlayerNotFoundException mpe) { 
     691                                canceled = new MPlayerPathDialog(shell).open(); 
     692                        } catch (Exception e) { 
     693                                tabItem.dispose(); 
     694                                canceled = true; 
     695                        } 
    685696                 
    686697                return singleVideo; 
    687698        } 
    688699         
    689         private Directory newDirectory() { 
     700        /* private Directory newDirectory() { 
    690701                CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE); 
    691702                Directory directory = new Directory(tabFolder, SWT.NONE, tabItem); 
     
    695706                 
    696707                return directory; 
    697         } 
     708        } */ 
    698709         
    699710        private DVD newDVD() { 
  • trunk/src/org/thestaticvoid/iriverter/GenericProgressDialog.java

    • Property svn:keywords set to Id
  • trunk/src/org/thestaticvoid/iriverter/MPlayerInfo.java

    r154 r157  
    196196         
    197197        public boolean videoSupported() { 
    198                 if (getFrameRate() == -1 || getDimensions().getWidth() == -1 || getDimensions().getHeight() == -1) 
    199                         return false; 
    200                  
    201                 return true; 
     198                return getFrameRate() != -1 && getDimensions().getWidth() != -1 && getDimensions().getHeight() != -1; 
    202199        } 
    203200} 
  • trunk/src/org/thestaticvoid/iriverter/SingleVideo.java

    r156 r157  
    3030 
    3131import java.io.*; 
     32import java.util.*; 
    3233 
    33 public class SingleVideo extends Composite implements SelectionListener, TabItemControl, SingleVideoInfo { 
     34public class SingleVideo extends Composite implements SelectionListener, TabItemControl, Job { 
    3435        private CTabItem tabItem; 
    35         private Text inputVideoInput, outputVideoInput; 
    36         private Button inputVideoSelect, outputVideoSelect; 
    37         private String syncInputVideo, syncOutputVideo; 
     36        private String inputVideo; 
     37        private MPlayerInfo inputVideoInfo; 
     38        private Text outputVideoInput; 
     39        private Button outputVideoSelect; 
     40        private String mplayerPath, outputVideoText; 
    3841         
    39         public SingleVideo(Composite parent, int style, CTabItem tabItem) { 
     42        public SingleVideo(Composite parent, int style, CTabItem tabItem, String inputVideo, String mplayerPath) throws Exception { 
    4043                super(parent, style); 
    4144                this.tabItem = tabItem; 
     45                this.inputVideo = inputVideo; 
     46                this.mplayerPath = mplayerPath; 
     47                 
     48                inputVideoInfo = new MPlayerInfo(inputVideo.toString(), mplayerPath); 
     49                if (!inputVideoInfo.videoSupported()) { 
     50                        MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_ERROR); 
     51                        messageBox.setText("Unsupported Video"); 
     52                        messageBox.setMessage("MPlayer does not recognize this type of video:\n" + new File(inputVideo).getName()); 
     53                        messageBox.open(); 
     54                        throw new Exception("Unsupported video"); 
     55                } 
    4256                 
    4357                InputStream is = getClass().getResourceAsStream("icons/singlevideo-16.png"); 
    4458                tabItem.setImage(new Image(getDisplay(), is)); 
    45                 tabItem.setText("New Single Video"); 
     59                tabItem.setText(new File(inputVideo).getName()); 
    4660                 
    4761                GridLayout gridLayout = new GridLayout(); 
     
    6478                singleVideoLabel.setLayoutData(gridData); 
    6579                 
    66                 /* Label tab = new Label(this, SWT.NONE); 
    67                 tab.setText("\t"); */ 
    68                  
    69                 Label inputVideo = new Label(this, SWT.NONE); 
    70                 inputVideo.setText("Input:"); 
    71                  
    72                 inputVideoInput = new Text(this, SWT.BORDER); 
    73                 inputVideoInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
    74                  
    75                 inputVideoSelect = new Button(this, SWT.PUSH); 
    76                 inputVideoSelect.setText("Select"); 
    77                 gridData = new GridData(); 
    78                 gridData.widthHint = 75; 
    79                 inputVideoSelect.setLayoutData(gridData); 
    80                 inputVideoSelect.addSelectionListener(this); 
    81                  
    82                 /* tab = new Label(this, SWT.NONE); 
    83                 tab.setText("\t"); */ 
    84                  
    8580                Label outputVideo = new Label(this, SWT.NONE); 
    8681                outputVideo.setText("Output:"); 
    8782                 
    8883                outputVideoInput = new Text(this, SWT.BORDER); 
     84                outputVideoInput.setText(inputVideo.substring(0, inputVideo.lastIndexOf('.')) + "." + ConverterOptions.getCurrentProfile().getProfileName() + "." + ConverterOptions.getCurrentProfile().getWrapperFormat()); 
    8985                outputVideoInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
    9086                 
     
    10197        } 
    10298         
    103         public void widgetSelected(SelectionEvent e) { 
    104                 if (e.getSource() == inputVideoSelect) { 
    105                         FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN); 
    106                         fileDialog.setText("Input Video"); 
    107                         fileDialog.setFilterExtensions(new String[]{"*.avi;*.vob;*.mkv;*.mpg;*.mpeg;*.mp4;*.ogm;*.mov;*.rm;*.ram;*.wmv;*.asf", "*.avi", "*.vob", "*.mkv", "*.mpg;*.mpeg;*.mp4", "*.ogm", "*.mov", "*.rm;*.ram", "*.wmv;*.asf", "*"}); 
    108                         fileDialog.setFilterNames(new String[]{"All Video Files", "AVI Video (*.avi)", "DVD Video Object (*.vob)", "Matroska Video (*.mkv)", "MPEG Video (*.mpg, *.mpeg, *.mp4)", "Ogg Video (*.ogm)", "Quicktime Movie (*.mov)", "Real Video (*.rm, *.ram)", "Windows Media Video (*.wmv, *.asf)", "All Files"}); 
    109                         String file = fileDialog.open(); 
    110                         if (file != null) { 
    111                                 inputVideoInput.setText(file); 
    112                                 outputVideoInput.setText(file.substring(0, file.lastIndexOf('.')) + "." + ConverterOptions.getCurrentProfile().getProfileName() + "." + ConverterOptions.getCurrentProfile().getWrapperFormat()); 
    113                                 tabItem.setText(new File(file).getName()); 
    114                         } 
    115                 } 
    116                  
     99        public void widgetSelected(SelectionEvent e) {           
    117100                if (e.getSource() == outputVideoSelect) { 
    118101                        FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE); 
     
    135118        } 
    136119         
    137         public void setInputVideo(String inputVideo) { 
    138                 tabItem.setText(new File(inputVideo).getName()); 
    139                  
    140                 inputVideoInput.setText(inputVideo); 
    141                 outputVideoInput.setText(inputVideo.substring(0, inputVideo.lastIndexOf('.')) + "." + ConverterOptions.getCurrentProfile().getProfileName() + ".avi"); 
    142         } 
    143          
    144         public synchronized String getInputVideo() {             
     120        private String getOutputVideo() { 
    145121                Display.getDefault().syncExec(new Runnable() { 
    146122                        public void run() { 
    147                                 syncInputVideo = inputVideoInput.getText(); 
     123                                outputVideoText = outputVideoInput.getText(); 
    148124                        } 
    149125                }); 
    150126                 
    151                 return syncInputVideo; 
     127                return outputVideoText; 
    152128        } 
    153129         
    154         public synchronized String getOutputVideo() { 
    155                 Display.getDefault().syncExec(new Runnable() { 
    156                         public void run() { 
    157                                 syncOutputVideo = outputVideoInput.getText(); 
    158                         } 
    159                 }); 
    160                  
    161                 return syncOutputVideo; 
     130        public String getDescription() {                 
     131                return "Converting " + new File(inputVideo).getName(); 
    162132        } 
    163133         
    164         public synchronized void setOutputVideo(String outputVideo) { 
    165                 syncOutputVideo = outputVideo; 
    166  
    167                 Display.getDefault().syncExec(new Runnable() { 
    168                         public void run() { 
    169                                 outputVideoInput.setText(syncOutputVideo); 
     134        public MencoderCommand[] getMencoderCommands() { 
     135                java.util.List mencoderCommandsList = new ArrayList(); 
     136                 
     137                 
     138                java.util.List commandList = MencoderCommand.prepareBaseCommandList(inputVideo, getOutputVideo(), mplayerPath, inputVideoInfo, 0); 
     139                String[] command = (String[]) commandList.toArray(new String[]{}); 
     140                mencoderCommandsList.add(new MencoderCommand("Encoding...", command)); 
     141                 
     142                int length = inputVideoInfo.getLength(); 
     143                String inputVideo = getOutputVideo(); 
     144                if (length > ConverterOptions.getSplitTime() * 60 && ConverterOptions.getAutoSplit()) { 
     145                        int pieces = (length / (ConverterOptions.getSplitTime() * 60)) + 1; 
     146                        for (int i = 0; i < pieces; i++) { 
     147                                String outputVideo = inputVideo.substring(0, inputVideo.lastIndexOf('.')) + ".part" + (i + 1) + ".avi"; 
     148                                 
     149                                if ((i + 1) == 1) 
     150                                        command = new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, inputVideo, "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-endpos", "" + (length / pieces)}; 
     151                                else if ((i + 1) == pieces) 
     152                                        command = new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, inputVideo, "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-ss", "" + (length / pieces) * i}; 
     153                                else 
     154                                        command = new String[]{mplayerPath + MPlayerInfo.MENCODER_BIN, inputVideo, "-o", outputVideo, "-ovc", "copy", "-oac", "copy", "-ss", "" + (length / pieces) * i, "-endpos", "" + (length / pieces)}; 
     155                                 
     156                                mencoderCommandsList.add(new MencoderCommand("Splitting Part " + (i + 1) + " of " + pieces, command)); 
    170157                        } 
    171                 }); 
     158                } 
     159                 
     160                return (MencoderCommand[]) mencoderCommandsList.toArray(new MencoderCommand[]{}); 
    172161        } 
    173162} 
Note: See TracChangeset for help on using the changeset viewer.