Reputation: 1758
I try to scale down video on Android device using FFMPEG
.
OnCreate
of Activity where I call scaleVideo()
, also initialization of FFMPEG
and binaries load.
private void scaleVideo(String path) throws FFmpegCommandAlreadyRunningException, IOException {
File copy = new File(DESTINATION_PATH + FilenameUtils.getBaseName(path) + "_scaled." + FilenameUtils.getExtension(path));
if (!copy.exists())
copy.createNewFile();
String cmd = "-i " + path + " -vf scale=320:-1 " + copy.getPath();
String[] command = cmd.split(" ");
fFmpeg.execute(command, new FFmpegExecuteResponseHandler() {
@Override
public void onSuccess(String message) {
Log.i("FFmpeg", message);
mProgressDialog.cancel();
runOnUiThread(() -> Toast.makeText(TrimmerActivity.this, getString(R.string.video_saved_at, copy.getPath()), Toast.LENGTH_SHORT).show());
VideoModel videoModel = new VideoModel();
videoModel.setPath(copy.getPath());
videoModel.save();
finish();
}
@Override
public void onProgress(String message) {
Log.i("FFmpeg", message);
}
@Override
public void onFailure(String message) {
Log.e("FFmpeg", message);
}
@Override
public void onStart() {
Log.i("FFmpeg", "on start");
}
@Override
public void onFinish() {
Log.i("FFmpeg", "on finish");
}
});
}
And as output I get:
I/FFmpeg: on start
D/FFmpeg: Running publishing updates method
I/FFmpeg: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
I/FFmpeg: built with gcc 4.8 (GCC)
I/FFmpeg: configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
I/FFmpeg: libavutil 55. 17.103 / 55. 17.103
I/FFmpeg: libavcodec 57. 24.102 / 57. 24.102
I/FFmpeg: libavformat 57. 25.100 / 57. 25.100
I/FFmpeg: libavdevice 57. 0.101 / 57. 0.101
I/FFmpeg: libavfilter 6. 31.100 / 6. 31.100
I/FFmpeg: libswscale 4. 0.100 / 4. 0.100
I/FFmpeg: libswresample 2. 0.101 / 2. 0.101
I/FFmpeg: libpostproc 54. 0.100 / 54. 0.100
I/FFmpeg: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/MP4_20161015_001403.mp4':
I/FFmpeg: Metadata:
I/FFmpeg: major_brand : iso6
I/FFmpeg: minor_version : 1
I/FFmpeg: compatible_brands: mp42iso6avc1isom
I/FFmpeg: creation_time : 2016-10-14 21:14:03
I/FFmpeg: Duration: 00:00:02.10, start: 0.000000, bitrate: 19564 kb/s
I/FFmpeg: Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 19403 kb/s, SAR 1:1 DAR 16:9, 14.75 fps, 15 tbr, 90k tbn, 180k tbc (default)
I/FFmpeg: Metadata:
I/FFmpeg: rotate : 90
I/FFmpeg: creation_time : 2016-10-14 21:13:52
I/FFmpeg: Side data:
I/FFmpeg: displaymatrix: rotation of -90.00 degrees
I/FFmpeg: Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 156 kb/s (default)
I/FFmpeg: Metadata:
I/FFmpeg: creation_time : 2016-10-14 21:13:52
Video output and input are correct. Files exist and video player can recognize input.
And that's all. CPU/GPU/Memory do nothing and no callbacks called.
That my first time using FFMPEG
, so I don't know what to do.
Upvotes: 3
Views: 937
Reputation: 307
I know I'm a little bit late, but the issue here is that the file you're trying to write the output to already exists. In case someone else experiences this, just delete the file or add the -y
argument. It will overwrite the existing file.
Upvotes: 4
Reputation: 1758
So here is a code solution to scale down video to any size and save it as a GIF
private void scaleVideo(String path) throws FFmpegCommandAlreadyRunningException, IOException {
File originalVideo = new File(path);
String filename = DESTINATION_PATH + FilenameUtils.getBaseName(path) + "_scaled."
+ "gif";
File copy = new File(filename);
int i = 0;
while (copy.exists()) {
copy = new File(String.format(Locale.getDefault(),
"%s/%s_scaled_(%d).gif", DESTINATION_PATH, FilenameUtils.getBaseName(path), i));
i++;
}
copy.createNewFile();
String cmd = "-y -v debug -i " + path + " -r 15 -vf scale=w=320:h=320:force_original_aspect_ratio=increase -threads 2 -gifflags +transdiff -y " + copy.getPath();
String[] command = cmd.split(" ");
File finalCopy = copy;
fFmpeg.execute(command, new FFmpegExecuteResponseHandler() {
@Override
public void onSuccess(String message) {
Log.i("FFmpeg", message);
}
@Override
public void onProgress(String message) {
String[] strings = message.split("\n");
for (String string : strings) {
Log.i("FFmpeg", string);
}
}
@Override
public void onFailure(String message) {
String[] strings = message.split("\n");
for (String string : strings) {
Log.e("FFmpeg", string);
}
}
@Override
public void onStart() {
Log.i("FFmpeg", "on start");
}
@Override
public void onFinish() {
Log.i("FFmpeg", "on finish");
}
});
}
Upvotes: 1