Flutter: How to use flutter_ffmpeg to add overlays like watermarks and texts to a video? - flutter

I try to implement the video_editing feature to my app, and I'd tried the Tapioca Package and the Video_Manipulation package but found that they both do not meet my criteria, so I put my last hope on the flutter_ffmpeg package.
But as I read through its official doc on pub.dev, not a thing on my mind but "WHAT THE HECK", I can't understand what those commands are used for, and though I can't find anything related to add widget overlays to a video. And almost no tutorial on the web that explains how to use it.
So if you successfully implemented adding watermarks/texts to a video with the ffmpeg package, please show me how. Thanks!~

ffmpeg -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=5:5,drawtext=text=:x=(w-0)/8:y=(h-4)/10:fontsize=64:fontcolor=white:ryanwangTV" -c:a copy -movflags +faststart output.mp4
ffmpeg -i video.mp4 -i logo.png
there are the video in cuestion to work and the png image that we want to apply how watermark
the video.mp4 has two "parts" a video and a audio file, remember it
the logo.png is a one image, but it consederer a "video" the duration is miliseconds.
how you call parts of video.mp4 and logo.png?
using mapping, for file 1) you will called [0] and for file 2 (logo.png) you will used [1]
if you want to use the video of video.mp4 you will call [0:v] and the video of png is [1:v]
for watermark use filter complex, to "mix" the image on the video
"[0:v][1:v]overlay=5:5,drawtext=text=:x=(w-0)/8:y=(h-4)/10:fontsize=64:fontcolor=white:ryanwangTV
[0:v][1:v] is the video of video.mp4 and image of logo.png
overlay=5:5 the first 5 is the main video, and the second 5 is the image to put on of the video.
x=(w-0)/8 : is the coordenada x y=(h-4)/10 : the coordenada y
fontsize=64 fontcolor=white and the ultimate word is text that you
want to draw in video
-c:a copy its mean: copy the audio of file 1
-movflags +faststart : is to fast start for users of internet on browsers
output.mp4 is the name final

//audio replace on video
- String commandToExecute ='-r 15 -f mp4 -i ${AllUrl.VIDEO_PATH} -f mp3 -i ${AllUrl.AUDIO_PATH} -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -t $timeLimit -y ${AllUrl.OUTPUT_PATH}';
//To combine audio with image
String commandToExecute = '-r 15 -f mp3 -i ${AllUrl.AUDIO_PATH} -f image2 -i ${AllUrl.IMAGE_PATH} -pix_fmt yuv420p -t $timeLimit -y ${AllUrl.OUTPUT_PATH}';
//overlay Image on video
String commandToExecute = "-i ${AllUrl.VIDEO_PATH} -i ${AllUrl.IMAGE_PATH} -filter_complex overlay=10:10 -codec:a copy ${AllUrl.OUTPUT_PATH}";
/// To combine audio with gif
String commandToExecute = '-r 15 -f mp3 -i ${AllUrl.AUDIO_PATH} -f gif -re -stream_loop 5 -i ${AllUrl.GIF_PATH} -y ${AllUrl.OUTPUT_PATH}';
/// To combine audio with sequence of images
String commandToExecute = '-r 30 -pattern_type sequence -start_number 01 -f image2 -i ${AllUrl.IMAGES_PATH} -f mp3 -i ${AllUrl.AUDIO_PATH} -y ${AllUrl.OUTPUT_PATH}';

Related

FFMPEG Video Merging Not Works for Quality Videos taken from mobile phones

I have used below commond for merging video
ffmpeg -f concat -safe 0 -i input.txt -copy out.mp4
but not working for some videos specially camera video's
ffmpeg -f concat -safe 0 -i input.txt -copy out.mp4
#input.txt - contains input1.mp4,input2.mp4
Can above commond will works for all video's

Issue regarding Slides not being shown in converted video to mp4 in BigBlueButton

We are using BigBlueButton 2.4 for webinars. When an webinar video is processed by bigbluebutton, the presentation shows the slides that were uploaded in the webiner, but the converted video that we are downloading, does not have those slides shown (Rest of the video is okay).
Does anyone know how to fix this for this particular version?
The code that we are using is mentioned below, if it helps:
#!/bin/sh
# Convert the deskshare and webcam to a combined video stream including logo
cd /var/bigbluebutton/published/presentation/
meetingId=$1
cd $meetingId
# add webcam sound to deskshare
if [ -e deskshare/deskshare.webm ]
then
ffmpeg -nostdin -threads 4 -i video/webcams.webm -i deskshare/deskshare.webm -af afftdn deskshare_with_sound.mp4
else
ffmpeg -nostdin -threads 4 -i video/webcams.webm -af afftdn deskshare_with_sound.mp4
fi
ffmpeg -nostdin -threads 4 -i video/webcams.webm -vf

FFMPEG Merge a video that has sound with an mp3 audio file outputs audio out of sync [duplicate]

I used a command like:
ffmpeg -i video.avi -i audio.mp3 -vcodec codec -acodec codec output_video.avi -newaudio
in latest version for adding new audio track to video (not mix).
But I updated the ffmpeg to the newest version (ffmpeg version git-2012-06-16-809d71d) and now in this version the parameter -newaudio doesn't work.
Tell me please how I can add new audio to my video (not mix) using ffmpeg.
Replace audio
ffmpeg -i video.mp4 -i audio.wav -map 0:v -map 1:a -c:v copy -shortest output.mp4
The -map option allows you to manually select streams / tracks. See FFmpeg Wiki: Map for more info.
This example uses -c:v copy to stream copy (mux) the video. No re-encoding of the video occurs. Quality is preserved and the process is fast.
If your input audio format is compatible with the output format then change -c:v copy to -c copy to stream copy both the video and audio.
If you want to re-encode video and audio then remove -c:v copy / -c copy.
The -shortest option will make the output the same duration as the shortest input.
Add audio
ffmpeg -i video.mkv -i audio.mp3 -map 0 -map 1:a -c:v copy -shortest output.mkv
The -map option allows you to manually select streams / tracks. See FFmpeg Wiki: Map for more info.
This example uses -c:v copy to stream copy (mux) the video. No re-encoding of the video occurs. Quality is preserved and the process is fast.
If your input audio format is compatible with the output format then change -c:v copy to -c copy to stream copy both the video and audio.
If you want to re-encode video and audio then remove -c:v copy / -c copy.
The -shortest option will make the output the same duration as the shortest input.
Mixing/combining two audio inputs into one
Use video from video.mkv. Mix audio from video.mkv and audio.m4a using the amerge filter:
ffmpeg -i video.mkv -i audio.m4a -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -ac 2 -shortest output.mkv
See FFmpeg Wiki: Audio Channels for more info.
Generate silent audio
You can use the anullsrc filter to make a silent audio stream. The filter allows you to choose the desired channel layout (mono, stereo, 5.1, etc) and the sample rate.
ffmpeg -i video.mp4 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-c:v copy -shortest output.mp4
Also see
Combine two audio streams into one
FFmpeg Wiki: Audio Channel Manipulation
FFmpeg mux video and audio from another video
mp3 music to wav
ffmpeg -i music.mp3 music.wav
truncate to fit video
ffmpeg -i music.wav -ss 0 -t 37 musicshort.wav
mix music and video
ffmpeg -i musicshort.wav -i movie.avi final_video.avi
If the input video has multiple audio tracks and you need to add one more then use the following command:
ffmpeg -i input_video_with_audio.avi -i new_audio.ac3 -map 0 -map 1 -codec copy output_video.avi
-map 0 means to copy (include) all streams from the first input file (input_video_with_audio.avi) and -map 1 means to include all streams (in this case one) from the second input file (new_audio.ac3).
None of these solutions quite worked for me. My original audio was being overwritten, or I was getting an error like "failed to map memory" with the more complex 'amerge' example. It seems I needed -filter_complex amix.
ffmpeg -i videowithaudioyouwanttokeep.mp4 -i audiotooverlay.mp3 -vcodec copy -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest -b:a 144k out.mkv
Nothing quite worked for me (I think it was because my input .mp4 video didn't had any audio) so I found this worked for me:
ffmpeg -i input_video.mp4 -i balipraiavid.wav -map 0:v:0 -map 1:a:0 output.mp4
This shows how to merge all audio tracks into one entire directory with ffmpeg:
ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -shortest output.mp4
Here is how I did what the OP wanted.
My setup is I have two stream of media one video (with its own audio channel) & one audio. I am not converting from but I am restreaming live source by integrating it with an additional audio channel without replacing the old audio from the video stream.
Here is the code I used.
ffmpeg -i "Video stream with its own audio" -i "another audio stream" -map 0:v -map 0:a -map 1:a -shortest -f mpegts "multicast udp stream out put"
what the code does is, it maps each video and audio channels after it acquired the streams from the live source. -map 0:v is the video stream, -map 0:a is the audio that is coming from the video source (notice the 0s from the -map) and finally -map 1:a which is the audio stream from the second source.
then it just restreams it using mpegts through a multicast address. You can change this to a file, a unicast stream or any other supported output format.
Here is the code I am using.
ffmpeg -i "rtp://#231.0.0.208:1234" -i "rtp://#231.0.0.206:1234" -map 0:v -map 0:a -map 1:a -shortest -f mpegts "udp://#231.0.0.45:1234"
Hope this helps some one. Thanks!
If you are using an old version of FFMPEG and you cant upgrade you can do the following:
ffmpeg -i PATH/VIDEO_FILE_NAME.mp4 -i PATH/AUDIO_FILE_NAME.mp3 -vcodec copy -shortest DESTINATION_PATH/NEW_VIDEO_FILE_NAME.mp4
Notice that I used -vcodec
Code to add audio to video using ffmpeg.
If audio length is greater than video length it will cut the audio to video length.
If you want full audio in video remove -shortest from the cmd.
String[] cmd = new String[]{"-i", selectedVideoPath,"-i",audiopath,"-map","1:a","-map","0:v","-codec","copy", ,outputFile.getPath()};
private void execFFmpegBinaryShortest(final String[] command) {
final File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/videoaudiomerger/"+"Vid"+"output"+i1+".mp4");
String[] cmd = new String[]{"-i", selectedVideoPath,"-i",audiopath,"-map","1:a","-map","0:v","-codec","copy","-shortest",outputFile.getPath()};
try {
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
#Override
public void onFailure(String s) {
System.out.println("on failure----"+s);
}
#Override
public void onSuccess(String s) {
System.out.println("on success-----"+s);
}
#Override
public void onProgress(String s) {
//Log.d(TAG, "Started command : ffmpeg "+command);
System.out.println("Started---"+s);
}
#Override
public void onStart() {
//Log.d(TAG, "Started command : ffmpeg " + command);
System.out.println("Start----");
}
#Override
public void onFinish() {
System.out.println("Finish-----");
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
// do nothing for now
System.out.println("exceptio :::"+e.getMessage());
}
}
The marked answer does not set the audio track's language.
The following is an example that specifies German for the default audio track (the video's only audio channel) and English for the audio track that is added anew:
ffmpeg -i "/dir/video.mkv" -i "/dir2/audio.ac3" -map 0 -map 1:a -c:v copy -shortest -metadata:s:a:0 language=ger -metadata:s:a:0 title="GER" -metadata:s:a:1 language=eng -metadata:s:a:1 title="ENG" "/dir/output.mkv"
(The s:a:0 starts counting from 0, adjust that number as needed. If the audio track language is already specified, you don't need to set it and would only need to set it for the audio track that you add.)

Trim mp4 files without encoding it again

I have a .mp4 video file, I need to trim it, however no matter how I do it, trimmed video is being encoded again which results in noisy video.
What I've tried:
Open video with Matlab, read frames and write only the frames that I want to have in trimmed video, I use 'MPEG-4' option.
Trim video using Windows Movie Maker.
Trim video using VirtualDub.
In first 2 scenarios original mp4 movie is encoded again after trimming it. I couldn't get mp4 files open in VirtualDub.
So what would be the easiest way to trim a video without re-encdong it?
You can do the split and re-encode in one command.
Create a text file, list.txt,
like this
file 'in.mp4'
inpoint 48.101
outpoint 67.459
file 'in.mp4'
inpoint 76.178
outpoint 86.399
file 'in.mp4'
inpoint 112.140
outpoint 125.031
then run,
ffmpeg -f concat -i list.txt -an -crf 18 out_merged.mp4
I've solved it with the following commands:
ffmpeg.exe -ss 48.101 -t 19.358 -i in.mp4 -an out_part1.mp4
ffmpeg.exe -ss 76.178 -t 10.221 -i in.mp4 -an out_part2.mp4
ffmpeg.exe -ss 112.140 -t 12.891 -i in.mp4 -an out_part3.mp4
ffmpeg -i out_part1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt1.ts
ffmpeg -i out_part2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt2.ts
ffmpeg -i out_part3.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt3.ts
ffmpeg -i "concat:intrmdt1.ts|intrmdt2.ts|intrmdt3.ts" -c copy out_merged.mp4
And some explanation:
Giving -ss (start time) and -t (duration) options before -i (input) option avoids unnecessary decoding.
Not using -c copy provides transcoding hence result more precise cut (got this from here).
I used -an because I didn't need the audio, if you need audio just omit this option.
Before concatenating the resulting trimmed videos I needed to transcode them to mpeg transport streams, to achieve lossless concatenation (for more details you can see this link).

Cut parts of subtitle file using FFMPEG

I want to edit an input mp4 file with audio video subtitle streams, i can successfully cut video and audio streams by:
1- Convert each part to a new mp4 file
2- Convert mp4 files to mpg files
ffmpeg -i out00.mp4 -qscale:v 0 intermediate1.mpg
ffmpeg -i out01.mp4 -qscale:v 0 intermediate2.mpg
3- Merge mpg files
ffmpeg -i concat:"intermediate1.mpg|intermediate2.mpg" -c copy intermediate_all.mpg
4- Convert the mpg file back to mp4
ffmpeg -i intermediate_all.mpg -f mp4 output.mp4
and i can extract the subtitle stream to srt file, but i can't cut parts of the subtitle file, do you have an idea how to do this?
Ok it works after long searching trip, here what i did:
1- Download latest FFMPEG from here (this was causing alot of problems for me).
2- Add the subtitle to the input files:
ffmpeg -i "input.mkv" -i subtitles.srt -c copy -c:s srt -metadata:s:s:1 language=eng -map 0 -map 1 -map_metadata 0 output.mkv
3- Cut the parts you need as you did normally ffmpeg will cut subtitle stream also
ffmpeg -i input.mkv -ss 00:00:00 -t 00:01:35 -c copy -map 0:0 -map 0:1 -map 0:2 -qscale:0:V 0 -y out0.mkv
4- Join the cut files using concat demux:
ffmpeg -f concat -i mylist.txt -c copy output.mkv
where mylist.txt contains:
file out0.mkv
file out1.mkv
And enjoy :).
I think you have to edit the srt manually (text and timecode the text needs to appear at) and remux it to your final video with a command like:
ffmpeg -i EditedSource.mp4 -i EditedSub.srt -c copy -map 0:a -map 0:v -map 1:s output.mp4
And instead of creating these intermediate files, have you tried:
ffmpeg -i out00.mp4 -i out01.mp4 -filter_complex '[0][1]concat=n=2:v=1:a=1 [v][a]' -map '[v]' -map '[a]' output.mp4