Mix audio/video of different lengths with ffmpeg - ffmpeg

I want to mix video from video.mp4 (1 minute duration) and audio from audio.mp3 (10 minute duration) into one output file with a duration of 1 minute. The audio from audio.mp3 should be from the 4 min - 5 min position. How can I do this with ffmpeg?

If video.mp4 has no audio
You can use this command:
ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c copy -shortest output.mkv
The audio will be from the 4 minute position (-ss 00:04:00) as requested in the question.
This example will stream copy (re-mux) the video and audio–no re-encoding will happen.
If video.mp4 has audio
You will have to add the -map option as described here: FFmpeg mux video and audio (from another video) - mapping issue.
If the audio is shorter than the video
Add the apad filter to add silent padding:
ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c:v copy -af apad -shortest output.mkv
Note that filtering requires re-encoding, so the audio will be re-encoded in this example.

Related

Convert mp4 video with joining animation mp4 + audio(wav, mp3) using ffmpeg shell command

I want to create .mp4 video by joining short animation(.mp4) + audio(wav, mp3) using ffmpeg shell command.
I have tried few commands to join mp4 + audio here is my example command which will generate output video.
./ffmpeg -y -i ./output/preview-2.mp4 -i ./output/audio-file.mp3 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -r 25 -vcodec libx264 -pix_fmt yuv420p ./output/converted-video.mp4
But I want to play my animation till audio playing. The above FFMPEG command only join my animation(short video .mp4) file with audio and stopped animation playing after duration of short video reached to end (let's say animation file duration is 10seconds and stopped playing after 10 seconds). But my audio is about 3 minutes and I want to re-play the animation video again and again till the audio file duration (3 minutes). Just like a looping effect.
Add -stream_loop -1 and -shortest:
./ffmpeg -y -stream_loop -1 -i ./output/preview-2.mp4 -i ./output/audio-file.mp3 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -r 25 -vcodec libx264 -pix_fmt yuv420p -shortest ./output/converted-video.mp4

ffmpeg add multiple mp3 files to mp4 video at particular position

It might be a duplicate question but didn't find any helpful answer.
I have 2 audio files and 1 mp4 video file. Want to add the 2 audio files to mp4 videoat specific time.
For example:
Video file:
input.mp4 (2 minutes video)
Audio files:
Audio File 1:
test_0:01.mp3 (15 seconds audio file) I want to insert this file at position 0:01 in the mp4 video
Audio File 2:
test_0:20.mp3(15 seconds audio file) I want to insert this file at position 0:20 in the mp4 video
I tried the following command with offset
It's only inserting test_0:01.mp3 at 0:01 position in the video file
But test_0:20.mp3 is not getting inserted at 0:020 position getting mute for this file no audio.
ffmpeg -i input.mp4 -itsoffset 01 -i test_0:01.mp3 -itsoffset 20 -i test_0:20.mp3 -c:v copy -map 0:v:0 -map 1:a -map 2:a -c:a aac -b:a 192k output.mp4
Any help will be appreciated!
Your command creates two audio tracks in the MP4 file. If you have a look in your video player you can choose between two audio tracks (usually used to choose different audio languages).
Why
This is because every -map parameter creates a new stream. In your example one video with two audio tracks.
Solution
Use the audio filter amix instead. Use also the filter adelay for the delay in the same filter chain to achieve the best result.
ffmpeg -i input.mp4 -i test_0:01.mp3 -i test_0:20.mp3 -filter_complex "[1:a]adelay=1s:all=1[a1];[2:a]adelay=20s:all=1[a2];[a1][a2]amix=inputs=2[amixout]" -map 0:v:0 -map "[amixout]" -c:v copy -c:a aac -b:a 192k output.mp4

Create background sound for video with ffmpeg

I have a video file without sound and a stereo audio file. Video is several times longer than audio. I'd like to create a background sound which starts from 2 second silence, then trim silence of the audio at both ends and duplicate trimmed audio several times to the end of the video.
I found how to trim audio:
ffmpeg -y -i audio.wav -af silenceremove=start_periods=1:start_threshold=-75dB,areverse,silenceremove=start_periods=1:start_threshold=-75dB,areverse trimmed_audio.wav
And how to create silence:
ffmpeg -y -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000:duration=2 silence.wav
How can I duplicate the audio and combine it with the video?
Basic syntax is
ffmpeg -an -i video -stream_loop -1 -i trimmed_audio.wav -af adelay=2000:all=1 -shortest -fflags +shortest -max_interleave_delta 100M out.mp4
Use ffmpeg 4.1 or newer.

Merge one audio file and one image file to create a video with ffmpeg

I first tried:
ffmpeg -y -i image.jpg -i audio.mp3 -c:a copy output.mp4
but when I uploaded to video sharing websites (bilibili.com), it says "no video track", so I tried:
ffmpeg -r 1 -loop 1 -i image.jpg -i audio.mp3 -acodec copy -r 1 -shortest output.mp4
The file was successfully uploaded, but when I watched it on the website, the image disappeared and it turned grey. I merged 6 videos and only one of them can be normally played back.
What should I do?
Problems with your command #2:
Frame rate is too low. Most players are unable to play 1 fps. Use 10 fps or higher for output, or set input -framerate to 10 fps or higher.
Chroma subsampling. Most players can only play 4:2:0, so use the format filter to force it to 4:2:0.
MP3 in MP4. Some players are unable to play MP3 in MP4. For highest compatibility use AAC. ffmpeg will choose AAC by default for MP4.
Faststart (optional). Add -movflags +faststart so MP4 can begin playback faster.
Command:
ffmpeg -framerate 1 -loop 1 -i image.jpg -i audio.mp3 -vf format=yuv420p -r 10 -shortest -movflags +faststart output.mp4

Use FFmpeg to combine fdshow audio capture with still image

On windows I am trying to capture direct show audio and combine with a still image. I have come up with the following command:
ffmpeg -f dshow -i audio="Microphone (Conexant SmartAudio HD)" -loop 1 -i black2.png -b:a 30k -ac 1 -acodec libfdk_aac -vcodec libx264 -b:v 60k -shortest test.mp4
This nearly works, a video with image and audio are produced but the video output is created at a much faster rate that the captured audio. So if the audio is capturing for 1 minute a 5 minute video is produced instead of 1 minute. The audio plays for the 1st minute and there is no audio for the remaining 4 minutes, The images displays through out video.
Any help appreciated, Thank you.
Try
ffmpeg -f dshow -i audio="Microphone (Conexant SmartAudio HD)" -loop 1 -re -i black2.png -b:a 30k -ac 1 -acodec libfdk_aac -vcodec libx264 -b:v 60k -shortest test.mp4
The -re limits the speed at which Ffmpeg processes the video - to realtime.

Resources