added webm support
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / EncodingTask.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.encoder;
11
12 import java.io.File;
13
14 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
15
16 public class EncodingTask {
17
18         /**
19          * Source file to encode
20          */
21         private final File source;
22
23         /**
24          * Target file.
25          */
26         private final File target;
27
28         private final AbstractConverter converter;
29
30         private boolean useTerminal;
31
32         private String targetFormat;
33
34         public EncodingTask(
35                         final File source,
36                         final File destination,
37                         final eu.svjatoslav.meviz.encoder.converters.AbstractConverter converter,
38                         String targetFormat) {
39
40                 this.source = source;
41                 target = destination;
42                 this.converter = converter;
43                 this.targetFormat = targetFormat;
44         }
45
46         /**
47          * @return the useTerminal
48          */
49         public boolean doUseTerminal() {
50                 return useTerminal;
51         }
52
53         public String encodeApostrophes(final String input) {
54                 final StringBuffer result = new StringBuffer();
55
56                 for (final char c : input.toCharArray()) {
57                         if (c == '\'') {
58                                 result.append("'\\''");
59                                 continue;
60                         }
61                         result.append(c);
62                 }
63
64                 return result.toString();
65         }
66
67         public void execute(final EncodingOptions encodingOptions) {
68                 try {
69                         String command = getCommand(encodingOptions);
70
71                         if (doUseTerminal())
72                                 command = "xterm -e '" + encodeApostrophes(command) + "'";
73
74                         System.out.println("Executing command: " + command);
75
76                         final Runtime run = Runtime.getRuntime();
77                         Process pr;
78                         pr = run.exec(new String[] { "/bin/bash", "-c", command });
79
80                         pr.waitFor();
81
82                 } catch (final Exception e) {
83                         System.out.println(e.toString());
84                         e.printStackTrace();
85                 }
86
87         }
88
89         public String getCommand(final EncodingOptions encodingOptions) {
90                 return converter.getCommand(source, target, encodingOptions,
91                                 targetFormat);
92         }
93
94         /**
95          * @param useTerminal
96          *            the useTerminal to set
97          */
98         public void setUseTerminal(final boolean useTerminal) {
99                 this.useTerminal = useTerminal;
100         }
101 }