Skip to content

Commit 00b2ee5

Browse files
committed
Enable encode button to process video with ffmpeg
1 parent 2bdd11a commit 00b2ee5

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

app/src/main/java/protect/videotranscoder/activity/MainActivity.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.Arrays;
4343
import java.util.Calendar;
4444
import java.util.Collections;
45+
import java.util.LinkedList;
4546
import java.util.List;
4647
import java.util.Locale;
4748
import java.util.Map;
@@ -173,6 +174,7 @@ public void onClick(View v)
173174
@Override
174175
public void onClick(View v)
175176
{
177+
startEncode();
176178
}
177179
});
178180
}
@@ -264,6 +266,125 @@ private void selectVideo()
264266
startActivityForResult(Intent.createChooser(intent, message), REQUEST_TAKE_GALLERY_VIDEO);
265267
}
266268

269+
private void startEncode()
270+
{
271+
MediaContainer container = (MediaContainer)containerSpinner.getSelectedItem();
272+
VideoCodec videoCodec = (VideoCodec)videoCodecSpinner.getSelectedItem();
273+
String fps = (String)fpsSpinner.getSelectedItem();
274+
String resolution = (String)resolutionSpinner.getSelectedItem();
275+
String videoBitrate = (String)videoBitrateSpinner.getSelectedItem();
276+
AudioCodec audioCodec = (AudioCodec) audioCodecSpinner.getSelectedItem();
277+
String audioBitrate = (String) audioBitrateSpinner.getSelectedItem();
278+
String audioSampleRate = (String) audioSampleRateSpinner.getSelectedItem();
279+
String audioChannel = (String) audioChannelSpinner.getSelectedItem();
280+
281+
File outputDir;
282+
283+
if(container.supportedVideoCodecs.size() > 0)
284+
{
285+
outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
286+
}
287+
else
288+
{
289+
outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
290+
}
291+
292+
String filePrefix = "Video_Transcoder_Output";
293+
String extension = "." + container.extension;
294+
String inputFilePath = videoInfo.file.getAbsolutePath();
295+
296+
File destination = new File(outputDir, filePrefix + extension);
297+
int fileNo = 0;
298+
while (destination.exists())
299+
{
300+
fileNo++;
301+
destination = new File(outputDir, filePrefix + "_" + fileNo + extension);
302+
}
303+
304+
outputDestination = destination;
305+
306+
List<String> command = new LinkedList<>();
307+
308+
// If the output exists, overwrite it
309+
command.add("-y");
310+
311+
// Input file
312+
command.add("-i");
313+
command.add(inputFilePath);
314+
315+
int startTimeSec = rangeSeekBar.getSelectedMinValue().intValue();
316+
if(startTimeSec != 0)
317+
{
318+
// Start time offset
319+
command.add("-ss");
320+
command.add(Integer.toString(startTimeSec));
321+
}
322+
323+
int endTimeSec = rangeSeekBar.getSelectedMaxValue().intValue();
324+
if( (videoInfo.durationMs)/1000 != endTimeSec)
325+
{
326+
// Duration of media file
327+
command.add("-t");
328+
command.add(Integer.toString(endTimeSec - startTimeSec));
329+
}
330+
331+
if(container.supportedVideoCodecs.size() > 0)
332+
{
333+
// Video codec
334+
command.add("-vcodec");
335+
command.add(videoCodec.ffmpegName);
336+
337+
// Frame size
338+
command.add("-s");
339+
command.add(resolution);
340+
341+
// Frame rate
342+
command.add("-r");
343+
command.add(fps);
344+
345+
// Video bitrate
346+
command.add("-b:v");
347+
command.add(videoBitrate + "k");
348+
}
349+
else
350+
{
351+
// No video
352+
command.add("-vn");
353+
}
354+
355+
// Audio codec
356+
command.add("-acodec");
357+
command.add(audioCodec.ffmpegName);
358+
359+
if(audioCodec == AudioCodec.VORBIS)
360+
{
361+
// The vorbis encode is experimental, and needs other
362+
// flags to enable
363+
command.add("-strict");
364+
command.add("-2");
365+
}
366+
367+
// Sample rate
368+
command.add("-ar");
369+
command.add(audioSampleRate);
370+
371+
// Channels
372+
command.add("-ac");
373+
command.add(audioChannel);
374+
375+
// Audio bitrate
376+
command.add("-b:a");
377+
command.add(audioBitrate + "k");
378+
379+
// Output file
380+
command.add(destination.getAbsolutePath());
381+
382+
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
383+
FFmpegUtil.call(command.toArray(new String[command.size()]), handler);
384+
385+
stopVideoPlayback();
386+
}
387+
267388
private void stopVideoPlayback()
268389
{
269390
videoView.pause();

0 commit comments

Comments
 (0)