Repository navigation
Expand file tree
/
Copy pathCrossfadeTest.java
More file actions
204 lines (138 loc) · 4.91 KB
/
Copy pathCrossfadeTest.java
File metadata and controls
204 lines (138 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package org.ffmpeg.android.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Locale;
import net.sourceforge.sox.CrossfadeCat;
import net.sourceforge.sox.SoxController;
import org.ffmpeg.android.Clip;
import org.ffmpeg.android.FfmpegController;
import org.ffmpeg.android.ShellUtils;
public class CrossfadeTest {
public static void test (String videoRoot, String fileTmpPath, String clipOutPath, double fadeLen) throws Exception
{
File fileTmp = new File(fileTmpPath);
File fileAppRoot = new File("");
File fileVideoRoot = new File(videoRoot);
String fadeType = "l";
int sampleRate = 22050;
int channels = 1;
FfmpegController ffmpegc = new FfmpegController (null, fileTmp);
Clip clipOut = new Clip();
clipOut.path = clipOutPath;
clipOut.audioCodec="aac";
clipOut.audioBitrate=56;
ArrayList<Clip> listVideos = new ArrayList<Clip>();
String[] fileList = fileVideoRoot.list();
for (String fileVideo : fileList)
{
if (fileVideo.endsWith("mp4"))
{
Clip clip = new Clip();
clip.path = new File(fileVideoRoot,fileVideo).getCanonicalPath();
//clip.startTime = "00:00:03";
//clip.duration = "00:00:02";
ffmpegc.getInfo(clip);
//System.out.println("clip " + fileVideo + " duration=" + clip.duration);
listVideos.add(clip);
}
}
//now add 1 second cross fade to each audio file and cat them together
SoxController sxCon = new SoxController(null, fileAppRoot, new ShellUtils.ShellCallback() {
@Override
public void shellOut(String shellLine) {
// System.out.println("sxCon> " + shellLine);
}
@Override
public void processComplete(int exitValue) {
if (exitValue != 0)
{
System.err.println("sxCon> EXIT=" + exitValue);
RuntimeException re = new RuntimeException("non-zero exit: " + exitValue);
re.printStackTrace();
throw re;
}
}
});
ArrayList<Clip> alAudio = new ArrayList<Clip>();
//convert each input file to a WAV so we can use Sox to process
int wavIdx = 0;
for (Clip mediaIn : listVideos)
{
if (new File(mediaIn.path).exists())
{
if (mediaIn.audioCodec == null)
{
//there is no audio track so let's generate silence
}
else
{
Clip audioOut = ffmpegc.convertToWaveAudio(mediaIn, new File(fileTmp, wavIdx+".wav").getCanonicalPath(),sampleRate,channels, new ShellUtils.ShellCallback() {
@Override
public void shellOut(String shellLine) {
// System.out.println("convertToWav> " + shellLine);
}
@Override
public void processComplete(int exitValue) {
if (exitValue != 0)
{
System.err.println("convertToWav> EXIT=" + exitValue);
RuntimeException re = new RuntimeException("non-zero exit: " + exitValue);
re.printStackTrace();
throw re;
}
}
});
alAudio.add(audioOut);
/*
float duration = (float) sxCon.getLength(new File(audioOut.path).getCanonicalPath());
if (mediaIn.duration == null)
{
mediaIn.duration = String.format(Locale.US, "%f", duration);
}*/
ffmpegc.getInfo(mediaIn);
wavIdx++;
}
}
else
{
throw new FileNotFoundException(mediaIn.path);
}
}
if (alAudio.size() > 0)
{
String fileOut = alAudio.get(0).path;
System.out.println("mix length=" + sxCon.getLength(fileOut));
for (int i = 1; i < alAudio.size(); i++)
{
File fileAdd = new File(alAudio.get(i).path);
CrossfadeCat xCat = new CrossfadeCat(sxCon, fileOut, fileAdd.getCanonicalPath(), fadeLen, fileOut);
xCat.start();
fileAdd.deleteOnExit();
System.out.println("mix length=" + sxCon.getLength(fileOut));
}
//1 second fade in and fade out, t = triangle or linear
//String fadeLenStr = sxCon.formatTimePeriod(fadeLen);
String fadeFileOut = sxCon.fadeAudio(fileOut, fadeType, fadeLen, sxCon.getLength(fileOut)-fadeLen, fadeLen);
//now export the final file to our requested output format mOut.mimeType = AppConstants.MimeTypes.MP4_AUDIO;
Clip mdFinalIn = new Clip();
mdFinalIn.path = fadeFileOut;
System.out.println ("final duration: " + sxCon.getLength(fadeFileOut));
Clip exportOut = ffmpegc.convertTo3GPAudio(mdFinalIn, clipOut, new ShellUtils.ShellCallback() {
@Override
public void shellOut(String shellLine) {
//System.out.println("convertTo3gp> " + shellLine);
}
@Override
public void processComplete(int exitValue) {
if (exitValue < 0)
{
RuntimeException re = new RuntimeException("non-zero exit: " + exitValue);
re.printStackTrace();
throw re;
}
}
});
}
}
}