copy audio (do not encode) when copying from memory card
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / EncodingTask.java
index 9db5d92..816fba7 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -11,49 +11,91 @@ package eu.svjatoslav.meviz.encoder;
 
 import java.io.File;
 
-import eu.svjatoslav.meviz.encoder.converters.Converter;
+import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
 
 public class EncodingTask {
 
-    /**
-     * Source file to encode
-     */
-    private final File source;
+       /**
+        * Source file to encode
+        */
+       private final File source;
 
-    /**
-     * Target file.
-     */
-    private final File target;
+       /**
+        * Target file.
+        */
+       private final File target;
 
-    private final Converter converter;
+       private final AbstractConverter converter;
 
-    private boolean useTerminal;
+       private boolean useTerminal;
 
-    public EncodingTask(final File source, final File destination,
-            final eu.svjatoslav.meviz.encoder.converters.Converter converter) {
+       private String targetFormat;
 
-        this.source = source;
-        target = destination;
-        this.converter = converter;
+       public EncodingTask(
+                       final File source,
+                       final File destination,
+                       final eu.svjatoslav.meviz.encoder.converters.AbstractConverter converter,
+                       String targetFormat) {
 
-    }
+               this.source = source;
+               target = destination;
+               this.converter = converter;
+               this.targetFormat = targetFormat;
+       }
 
-    /**
-     * @return the useTerminal
-     */
-    public boolean doUseTerminal() {
-        return useTerminal;
-    }
+       /**
+        * @return the useTerminal
+        */
+       public boolean doUseTerminal() {
+               return useTerminal;
+       }
 
-    public String getCommand(final EncodingOptions encodingOptions) {
-        return converter.getCommand(source, target, encodingOptions);
-    }
+       public String encodeApostrophes(final String input) {
+               final StringBuffer result = new StringBuffer();
 
-    /**
-     * @param useTerminal
-     *            the useTerminal to set
-     */
-    public void setUseTerminal(final boolean useTerminal) {
-        this.useTerminal = useTerminal;
-    }
+               for (final char c : input.toCharArray()) {
+                       if (c == '\'') {
+                               result.append("'\\''");
+                               continue;
+                       }
+                       result.append(c);
+               }
+
+               return result.toString();
+       }
+
+       public void execute(final EncodingOptions encodingOptions) {
+               try {
+                       String command = getCommand(encodingOptions);
+
+                       if (doUseTerminal())
+                               command = "xterm -e '" + encodeApostrophes(command) + "'";
+
+                       System.out.println("Executing command: " + command);
+
+                       final Runtime run = Runtime.getRuntime();
+                       Process pr;
+                       pr = run.exec(new String[] { "/bin/bash", "-c", command });
+
+                       pr.waitFor();
+
+               } catch (final Exception e) {
+                       System.out.println(e.toString());
+                       e.printStackTrace();
+               }
+
+       }
+
+       public String getCommand(final EncodingOptions encodingOptions) {
+               return converter.getCommand(source, target, encodingOptions,
+                               targetFormat);
+       }
+
+       /**
+        * @param useTerminal
+        *            the useTerminal to set
+        */
+       public void setUseTerminal(final boolean useTerminal) {
+               this.useTerminal = useTerminal;
+       }
 }