How to add text in video without using ffpeg Android? - android

I am using MediaMuxer and MediaExtractor to transcode video. Now i want to add text on video at certain position. How can i achieve this? Basically i am recording video and transcode it in lower resolution now i want to add text overlay without using ffmpeg. I tried to add watermark by using ffmpeg but it takes too much time. I am using this library.This is my command:
String[] complexCommand = {"-y" ,"-i", "/storage/emulated/0/Download/VID.mp4","-strict",
"experimental", "-vf", "movie="
+"/storage/emulated/0/Download/iqm_logo.png [watermark]; [in][watermark] overlay=main_w/2-overlay_w/2:450 [out]",
"-b:v", "1540k", "-c:v", "libx264","-threads","30","-preset","ultrafast","-r", "30","-s", "1080x1920","-c:a", "copy",
"/storage/emulated/0/Download/watermark.mp4"};

Related

Overlay text using FFmpeg command in android programmatically

I need to know what is the issue with my FFmpeg command for overlaying text to a video in android.
command = new String[]{"ffmpeg", "-i", original_path, "-vf", "drawtext=text='SiteName.local': fontsize=18: fontcolor=white: x=10:y=h-th-10", "-acodec", "copy", "-y", dest.getAbsolutePath()};
I am trying to create a video with a text overlay. However, i'm getting an error
[NULL # 0xea699600] Unable to find a suitable output format for 'ffmpeg'
ffmpeg: Invalid argument
I have tested for the input file and output file using different command for trimming videos and it worked. However, the FFmpeg command for overlaying text does not work. I kindly ask for help.
Besides, i also need to know how i can animate the text to scroll from left to right, bounce using FFmpeg commands in android etc
Finally i managed solving the issue over a long search. Drawtext can't work without including the fontfile. So the below code is what managed to settle my scores
command = new String[]{"-i", original_path, "-vf", "drawtext=fontfile=/system/fonts/DroidSans.ttf:text='SiteName hulluway':fontsize=40:fontcolor=black: x=w-(t-4.5)*(w+tw)/5.5:y=100", "-acodec", "copy", "-y", dest.getAbsolutePath()};

Concatenating videos using concat demuxer in FFMPEG is finished but video is lost in second video?

Used concat demuxer in FFMPEG code to concat videos :-
cmd = arrayOf("-y", "-f", "concat", "-safe", "0", "-i", textFile!!.path, "-c:v", "libx264", "-crf", "23", "-preset", "ultrafast", "-c:a", "copy", outputFile.path)
After concatenate finished, the video one played correctly has both audio & video but video two - video is missing has only audio. Output file has size & time duration including two videos.
The textFile contains :-
file '/path/to/file1'
file '/path/to/file2'
Used absolutePath for two mp4 video files.
Tryed this FFMPEG concat video finished but the video missing. But no solution to my problem.
Anything I am missing in the command.

Speed Up Video Processing time FFMPEG while adding multiple images to Video

I am trying to overlay multiple images to the video at certain time interval...
but the processing time is too much for a video length of 1 minute.
I used VideoKit Library for it.
Here is my code for adding multiple images to Video.
String[] command = {"-i", inputPath,
"-i", imagePath1,"-i", imagePath2,"-i",imagePath3,
"-filter_complex",
"[0][1]overlay=y=H-h:enable='between(t,2,10)'[v1];
[v1][2]overlay=y=H-h:enable='between(t,10,20)'[v2];
[v2][3]overlay=y=H-h:enable='between(t,20,30)'[v3]",
"-map", "[v3]", outputPath};
Is there any faster processing library for video processing.
I just added two more tags to make processing faster.
String[] command = {"-i", inputPath,
\"-i", imagePath1,"-i", imagePath2,"-i",imagePath3,
"-filter_complex",
"[0][1]overlay=y=H-h:enable='between(t,2,10)'[v1];
[v1][2]overlay=y=H-h:enable='between(t,10,20)'[v2];
[v2][3]overlay=y=H-h:enable='between(t,20,30)'[v3]",
"-map", "[v3]",
**"-preset", "ultrafast",** outputPath};

Create video from images, add audio and animation within images without FFMPEG in Android

I want to create a video from multiple images without using FFMPEG in Android. Also i have to multiple audios over it. And at last there should be some animation within each images. I have tried this using FFMPEG, but it is taking hell lot of time, let say , 4-5 min for 10 photos. Is there any other way for this or any efficient way?
There is 3 way process.
First i create video from each image with audio and added text.
Then i concatenate all the videos into a single video.
At last, i add a background audio to the concatenated video.
I am sharing the commands here:
1.
String[] cmds = {"-y"
, "-r", "1", "-vcodec", "mjpeg" ,"-loop", "1", "-t", "1"
, "-i", image
, "-i", audio
, "-vf"
, "zoompan=z='if(lte(zoom,1),1.1,max(1.001,zoom-0.0015))':d=125,drawbox=y=ih/PHI:color=black#0.4:width=iw:height=120:t=max,drawtext=fontfile=/storage/emulated/0/ffmpeg/font.ttf:text="+text+":fontcolor=white:fontsize=60:x=(w-tw)/2:y=(h/PHI)+th"
, "-c:v", "libx264"
, "-preset", "ultrafast", "-t", "5", "-c:a", "aac", "-b:a", "128k", "-pix_fmt", "yuv420p"
, "video1.mp4"};
2.
String cmd = "-y -i video1.mp4 -i video2.mp4 -i video3.mp4 -filter_complex concat=n=3:v=1:a=1 -preset ultrafast concat_video.mp4;
3.
cmd = "-y -i bg_audio.mp3 -i concat_video.mp4 -shortest -preset ultrafast -filter_complex [a1][a2]amerge,pan=stereo:c0

Reverse video in android

I have recorded a video from camera in my app and saved in device storage.Now I want to reverse the video such that it plays from backwards.i.e. if video is of 10 seconds then the last frame at 10th second will become first frame and it starts playing from there to 1st second first frame.I want to save the reversed video in a file.How should i proceed in that?
If you are prepared to use ffmpeg you can use this approach - it essentially breaks the video into frames and then builds it again in reverse order:
https://stackoverflow.com/a/8137637/334402
There are several ways to use ffmpeg in Android but the 'wrapper' approach is one which I have found a reasonable blend of performance and ease of use. Some example Android ffmpeg wrapper:
http://hiteshsondhi88.github.io/ffmpeg-android-java/
https://github.com/guardianproject/android-ffmpeg
It's worth being aware that this will be time-consuming on a Mobile - if you have the luxury of being able to upload to a server and doing the reversal there it might be quicker.
Thanks to Mick for giving me an idea to use ffmpeg for reversing video.
I have posted code at github for reversing video along with performing other video editing operation using ffmpeg and complete tutorial in my blog post here.
As written in my blog post,
For reversing video,first we need to divide video into segments with
duration of 10 seconds or less because reverse video command for
ffmpeg will not work for long duration videos unless your device has
32 GB of RAM.
Hence,to reverse a video-
1.Divide video into segments with duration of 10 seconds or less.
2.Reverse the segmented videos
3.Concatenate reversed segmented videos in reverse order.
For dividing video into segments with duration of 6 seconds we can use
the below command-
String[] complexCommand = {"-i", inputFileAbsolutePath, "-c:v",
"libx264", "-crf", "22", "-map", "0", "-segment_time", "6", "-g", "9",
"-sc_threshold", "0", "-force_key_frames", "expr:gte(t,n_forced*6)",
"-f", "segment", outputFileAbsolutePath};
Here,
-c:v libx264
encodes all video streams with libx264
-crf
Set the quality for constant quality mode.
-segment_time
time for each segment of video
-g
GOP size
-sc_threshold
set scene change threshold.
-force_key_frames expr:gte(t,n_forced*n)
Forcing a keyframe every n seconds
After segmenting video,we need to reverse the segmented videos.For
that we need to run a loop where each segmented video file will be
reversed.
To reverse a video with audio(without removing its audio) we can use
the below command-
String command[] = {"-i", inputFileAbsolutePath, "-vf", "reverse",
"-af", "areverse", outputFileAbsolutePath};
To reverse a video with audio removing its audio we can use the below
command-
String command[] = {"-i", inputFileAbsolutePath, "-an", "-vf",
"reverse", outputFileAbsolutePath};
To reverse a video without audio we can use the below command-
String command[] = {"-i",inputFileAbsolutePath, "-vf", "reverse",
outputFileAbsolutePath};
After reversing segmented videos,we need to concatenate reversed
segmented videos in reverse order.For that we sort videos on the basis
of last modified file using Arrays.sort(files,
LastModifiedFileComparator.LASTMODIFIED_REVERSE).
Then, to concatenate reversed segmented videos(with audio) we can use the below
command-
String command[] =
{"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath
.....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:v0] [0:a0]
[1:v1] [1:a1]...[N:vN] concat=n=N:v=1:a=1 [v]
[a],"-map","[v]","-map","[a]", outputFileAbsolutePath};
To concatenate reversed segmented videos(without audio) we can use the below
command-
String command[] =
{"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath
.....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:0] [1:0]
[2:0]...[N:0] concat=n=N:v=1:a=0",outputFileAbsolutePath};
Here,
-filter_complex [0:v0] [0:a0] [1:v1] [1:a1]…[N:vN] tells ffmpeg what streams to send to the concat filter.In the above case, video stream 0
[0:v0] and audio stream 0 [0:a0] from input 0,video stream 1 [1:v1]
and audio stream 1 [1:v1] from input 1 and so on.
concat filter is used to concatenate audio and video streams, joining
them together one after the other.The filter accepts the following
options:
n
Set the number of segments. Default is 2.
v
Set the number of output video streams, that is also the number of
video streams in each segment. Default is 1.
a
Set the number of output audio streams, that is also the number of
audio streams in each segment. Default is 0.

Categories

Resources