Add path to ffmpeg - android

I am playing with video compression by using ffmpeg. I can compress the video e.g. with this line:
String commandStr = "ffmpeg -y -i /sdcard/videokit3/Dani.mp4 -strict experimental -s 320x240-r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 512k /sdcard/videokit3/Dani2.mp4";
In this line the path is hardcoded, so I did
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "videokit3/Dani.mp4";
which results in /mnt/sdcard/videokit3/Dani.mp4
This seems to be different from /sdcard/videokit3/Dani.mp4 so this line runs to error (I see the difference, I just don't get it):
String commandStr = "ffmpeg -y -i " + path + " -strict experimental -s 320x240 -r 30 -ab 48000 -ac 2 -ar 22050 -b 512k /sdcard/videokit3/Dani4.mp4";
Can you help?

The easiest way to get around would be replace the string and trim "/mnt" out before inserting into the statement. The reason why the external SDcard absolute path returns as "mounted" drive is available here on SO: stackoverflow.com/a/5695129/2777098

Related

How to Add Watermark like tic tok using ffmpeg in android studio

I am trying To add Watermark Like Tic Tok using FFMPEG but it's Not working
GeneralUtils.checkForPermissionsMAndAbove(MainActivity.this, true);
LoadJNI vk = new LoadJNI();
try {
//String complexCommand = "ffmpeg -y -i /storage/emulated/0/in.mp4 -strict experimental -vf transpose=1 -s 160x120 -r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 2097k /storage/emulated/0/out.mp4";
/*String complexCommand = "ffmpeg -i /storage/emulated/0/in.mp4 -framerate 30000/1001 -loop 1 -i /storage/emulated/0/abcd.jpg -filter_complex\n" +
" \"[1:v] fade=out:st=30:d=1:alpha=1 [ov]; [0:v][ov] overlay=10:10 [v]\" -map \"[v]\"\n" +
" -map 0:a -c:v libx264 -c:a copy -shortest /storage/emulated/0/out.mp4";*/
//String complexCommand = "ffmpeg -y -i /storage/emulated/0/in.mp4 -strict experimental -vf movie=/storage/emulated/0/abcd.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out] -s 320x240 -r 30 -b 15496k -vcodec mpeg4 -ab 48000 -ac 2 -ar 22050 /storage/emulated/0/out.mp4";
String complexCommand = "ffmpeg -i /storage/emulated/0/in.mp4 -i /storage/emulated/0/abcd.jpg -filter_complex \\\n" +
"\"overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2\" \\\n" +
"-codec:a copy /storage/emulated/0/out.mp4";
String workFolder = getApplicationContext().getFilesDir().getAbsolutePath();
//String[] complexCommand = {"ffmpeg","-i", "/sdcard/videokit/in.mp4"};
vk.run(GeneralUtils.utilConvertToComplex(complexCommand) , workFolder , getApplicationContext());
Log.i("test", "ffmpeg4android finished successfully");
} catch (Throwable e) {
Log.e("test", "vk run exception.", e);
}
but noting work for me
as you can i have no knowledge about FFMPEG so if you give answere please write in brief.
thank you.
Its nothing to do with android but below are simple ffmpeg commands
Top-Left To Bottom-Right loop:
ffmpeg -i video.mp4 -i watermark.png -filter_complex \
"[0:v][1:v]overlay=x='if(lt(mod(t,10),5),10,W-w-10)':y='if(lt(mod(t,10),5),10,H-h-10)'" \
-codec:a copy out.mp4
Top-Right To Bottom-Left loop:
ffmpeg -i video.mp4 -i watermark.png -filter_complex \
"[0:v][1:v]overlay=x='if(lt(mod(t,10),5),W-w-10,10)':y='if(lt(mod(t,10),5),10,H-h-10)'" \
-codec:a copy out.mp4
Idea is very simple
Take every 10sec duration, get remainder sec i.e. mod(t,10)
if sec < 5, set top position else bottom position
START HERE https://drive.google.com/file/d/0B2aT0QoEmtuaN0VJZ2Z4ODY3T2s/view
File => New => import module => go find the folder downloaded at step1:
ffmpeg4android_demo_studio\ffmpeg4android_lib
Add permission in AndroidManifest.xml
if the version is higher than Android 6, add the following code in onCreate() of your Activity
GeneralUtils.checkForPermissionsMAndAbove(MainActivity.this, false);
Alt+Enter if encounter red font, AndroidStudio would help you to modify "build.gradle(Module)"
(Not sure if necessary) In build.gradle(Module)
Android{
.......
defaultConfig{
....
targetSdkVersion ...
ndk{
abiFilter "armeabi-v7a"
}
}
}
.....
dependencies{
...
compile project(':ffmpeg4android_lib')
}
add following code in "gradle.properties"
android.useDeprecatedNdk=true
and then async project
How to use WaterMarkHelper(by Johnny Tu) to add watermark on video
After import "WaterMarkHelper.java" in your project, declare the following in your Activity
WaterMarkHelper myHelper;
initialize myHelper with arguments
Arguments description:
String inputVideoPath (ex:"/sdcard/input.mp4")
String inputWaterMarkPath (ex:"/sdcard/waterMark.png")
String outputVideoPath (ex:"/sdcard/output.mp4")
String waterMarkX the X-coordinate
Video width: main_w, Video height: main_h, Image Width: overlay_w, Image Height: overlay_h
String waterMarkY
Download This

Video Compression using ffpmeg

I am using ffmeg for video compression. I have used ffpmeg library
Use command "ffmpeg -i " + filein.trim() + " -vcodec h264 -acodec mp2 " + fileout.trim() but i doesn't respond anything.
I have used any ndk library for first time.
Use this command will work for you. This works for me .
String cmd = "-y -i " + currentInputVideoPath + " -strict -2 -vcodec libx264 -preset ultrafast " + "-crf 24 -acodec aac -ar 44100 -ac 2 -b:a 96k -s 320x240 -aspect 4:3 " + currentOutputVideoPath;

ffmpeg video rotation issue

I am using ffmpeg library to compress video and upload it to server.
My problem is :
Video is saved with perfect rotation in storage but when I upload it to server it gets 90degree rotated.
Command is:
setCommand("ffmpeg -y -i /sdcard/videokit/" + NetworkStatus.OrderNo
+ ".mp4 -strict experimental -vcodec libx264 -preset ultrafast -crf 24 -acodec aac -ar 44100 -ac 2 -b:a 96k -s 320x240 /sdcard/videokit/" + NetworkStatus.OrderNo + "out.mp4");
Can you please help me to get out of this. I need to show video in portrait mode only if I have captured video in portrait.
Thanks

How to increase compression speed for FFmpeg?

I have used following command for compress video in android.
ffmpeg -y -i /sdcard/DCIM/Camera/VID_20150326_125017.mp4 -strict experimental -s 640x360 -r 25 -vcodec mpeg4 -b 1000k -ab 48000 -ac 2 -ar 22050 /sdcard/videokit/out.mp4
Above command is a work properly, but it's taking too much time for compression.
An 80MB video file takes around 3 minutes to compress to an 8MB file. Is there any way I can reduce this time.
Try this.
-y -i $sourceFile -s ${width}x${height} -r 5 -c:v libx264 -b:v 600k -b:a 44100 -ac 2 -ar 22050 -tune fastdecode -preset ultrafast $destPath
This command uses libx264 codec and -r 5 reduces the time taken.
-tune fastdecode -preset ultrafast this is also used for faster compression.
Reference ffmpeg preset
Result :
SOURCE SIZE : 63.7 MB DEST SIZE : 5.06MB
Time Taken : 15 Secs
Try the command mentioned below to compress videos. In my situation, 52 MB of video was compressed to 12 MB in about 19 seconds.
function command(input, output, bitrate) {
return new Promise((resolve, reject) => {
ffmpeg(input)
.outputOptions(['-c:v libx264', `-b:v ${bitrate}k`, '-c:a aac', '-b:a 58k','-crf 35'])
.output(output)
.on('start', (command) => {
console.log('TCL: command -> command', command)
})
.on('progress', function (progress) {
console.log('Processing: ' + progress.percent + '% done');
})
.on('error', (error) => reject(error))
.on('end', () => resolve())
.run()
})
}
Try the below command for video compression
In my case, i had video of 82mb and compressed to 5mb within around 50secs.
String[] command = new String[]{"-i", videoPath, "-vcodec", "h264", "-b:v", "1000k", "-acodec", "mp3" ,"-preset", "ultrafast", outputPath};
Hope it works!

Android ffmpeg apply video effects take log time

I am using below command to apply effect,its take 6-8 minutes to apply effect for 2 min video. Please help me to improve effective time for ffmpeg commands.
String commandStr = "ffmpeg -y -i "
+ mVideoData.getPath()
+ " -strict experimental -vf curves=vintage -s 640x480 -r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 2097k -vcodec mpeg4 /sdcard/videokit/curve.mp4";

Categories

Resources