I'm looking for a solution in FFmpeg to merge audio (mp3) with a short video loop, or gif.
I've already been able to generate a video from an image by joining with audio, but the video stays static frame for the audio duration, the command to make this:
ffmpeg -loop 1 -i imagem.jpg -i audio.mp3 -vcodec h264 -tune stillimage -acodec aac -b:a 64k -pix_fmt yuv420p -shortest video.mp4
I need video that has the duration of the audio, but that uses a loop of another mp4 or a gif. To keep repeating for the duration of the audio.
To do this with a video (MP4 or other format) you should use the Concatenate demuxer.
First create a text file with a list of the paths of the videos you want to concatenate. In your case it will be a list of the same video file, like the following.
# mylist.txt
file /your/path/video.mp4
file /your/path/video.mp4
file /your/path/video.mp4
The paths can be absolute or relative.
Then you need to use the concat demuxer option.
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
This will generate an mp4 with your original video looping 3 times. If your original video is 4 seconds long, then the output will be 12 seconds long. I suggest that you create a video just a bit longer than your audio track and then use the -shortest option when creating your final video.
You can add the audio within this same command like you do in your post. So, all together will look like this:
ffmpeg -f concat -safe 0 -i mylist.txt -i audio.mp3 -c:v copy -c:a copy -shortest output.mp4
In my example I do a stream copy for my output (this will work just fine and will be very fast), but you can use the codecs you want for yours (like H264 and AAC like your post).
You can find more info in the concat demuxer documentation or better yet the concat wiki.
At the moment I don't know if there's a way to do this with a gif file.
Related
I have the following problem.
In the folder there is video.mp4 file (contain 1 audio stream). There are also 3 different files audio1.wav, audio2.wav, audio3.wav. These files I need to 'attach' as multi stream to the video file - so the user can choose the audio language in VLC player or similar. Result must be one audio at the time - no mixing it all together.
Now, I've done it via Premiere Pro with multitrack (quicktime export to mov), and then I run a script to change audio stream names to correspond with the audio language (iso 639-2 ) and output the mp4 file. All works well, but I wonder if there is simple way to do everything via ffmpeg ( .bat script ). I have a working script for replacing audio in the video, but I need to add few additional .wav to the video file as separate audio tracks.
Any help will be appreciated!
To add a new audio track into an existing video with audio, use
the -i parameter to specify all the input files (original video and additional audios)
the -map option to manually select the tracks of each input (https://trac.ffmpeg.org/wiki/Map)
in your case,
-map 0 to copy all streams from the input #0 (video)
-map 1:a to include all audio streams from input#1 file (audio1)
-map 2:a to include all audio streams from input#2 file (audio2)
and so on
and
-shortest to crop the output to the shortest input
and additionally you may want to use
-c:v copy to copy the video stream without reencoding.
so, try this (line split for readability)
ffmpeg -i video.mp4 -i input1.mp3 -i input2.mp3
-map 0 -map 1:a -map 2:a
-c:v copy -shortest
output.mp4
and (addording to your comment) adding metadata for the audio tracks
ffmpeg -i video.mp4 -i input1.mp3 -i input2.mp3
map 0 -map 1:a -map 2:a
-metadata:s:a:0 language=eng
-metadata:s:a:1 language=ger
-metadata:s:a:2 language=fra
-disposition:a:0 default
-c:v copy -shortest
output.mp4
everybody here! So basically this is what I want to achieve:
I have a muted video about 3 minutes long.
I have a list of audio tracks in mp3 format (40 songs in a folder with duration 2 to 6 mins each one)
I want this video to play cycled automatically taking songs from playlist and injecting them to the video one by one. Every time a song finishes the next one from the list should start playing at the moment. Video continues playing and doesn't care duration of tracks.
I consider it as the first step on the way to broadcast radio with a video background on youtube in 24/7 mode with ability to put additional tracks to playlist without need to stop translation.
My problem is that I'm new in FFmpeg and I would appreciate any suggestions regarding which FFMpeg topic to start investigate with in order to achieve my goal
Use the concat demuxer
You can do live updates to the playlist for the concat demuxer, but each audio file must have the same attributes, the same number of streams, and all be the same format.
Create input.txt containing:
ffconcat version 1.0
file 'audio1.mp3'
file 'audio2.mp3'
file 'audio3.mp3'
file 'audio40.mp3'
All file names must be "safe" or it will fail with Unsafe file name. Basically no special characters in file names and only use absolute paths. See concat demuxer for more info.
Run ffmpeg to stream to YouTube:
ffmpeg -re -framerate 10 -loop 1 -i image.jpg -re -f concat -i input.txt -map 0:v -map 1:a -c:v libx264 -tune stillimage -vf format=yuv420p -c:a aac -g 20 -b:v 2000k -maxrate 2000k -bufsize 8000k -f flv rtmp://youtube
When you are ready to add new songs make temp.txt containing:
ffconcat version 1.0
file 'audio41.mp3'
file 'audio42.mp3'
file 'audio43.mp3'
Replace input.txt atomically:
mv temp.txt input.txt
See FFmpeg Wiki: Concatenate for lots more info.
If your audio files are not the same
The files listed in input.txt must all have the same:
Format (AAC, MP3, etc, but not mixed)
Sample rate (48000, 44100, etc)
Number of channels (mono, stereo, etc).
If they vary then you will have to pre-process them before adding them to the playlist. Bash example conforming each audio to stereo (-ac 2) with 44100 sample rate (-ar 44100) and save as AAC format in M4A container:
mkdir conformed
for f in *.mp3; do ffmpeg -i "$f" -map 0:a -ac 2 -ar 44100 -c:a aac "conformed/${f%.*}.m4a"; done
Outputting to AAC is recommended for streaming to YouTube.
If you do this then you can avoid re-encoding the audio in the ffmpeg command to YouTube. Just change -c:a aac to -c:a copy in step #2: Run ffmpeg to stream to YouTube.
What I want to achieve
I have two input videos which I do want to concatenate using ffmpeg.
Issue
The input videos contain two stereo audio channels:
after concatenation, the second audio stream is lost:
steps to reproduce
The Command I used is
ffmpeg -f concat -i list.txt -c:a copy -c:v copy demuxed.mp4
Additional Information
The Console Output shows the second audio stream in input properly:
The Output settings also show that there is only one audio output stream. But I want to have the exact same settings in to out, just concatenated:
Assuming that all input videos have matching audio streams, add mapping.
ffmpeg -f concat -i list.txt -map 0:v -map 0:a -c:a copy -c:v copy demuxed.mp4
See http://ffmpeg.org/ffmpeg.html#Stream-selection, especially the section on automatic stream selection for details on how ffmpeg selects streams.
I am trying to take a video extract the frames and the audio.Then i do some compression on the frames and then i want to combine the image frames with the audio to create the video.
1.Create frames from video based on fps (lossless)
ffmpeg -i big_buck_bunny_480p_surround-fix.avi -q:v 1 ./vidtest/out%d.jpg
The problem here is that the quality of the image is 94 based on graphicsmagick , how can i extract the frames at original quality.
2.Getting the audio
ffmpeg -i big_buck_bunny_480p_surround-fix.avi -vn -acodec copy big_buck_bunny_480p_surround-fix.ac3
3.How do i combine the audio with the image sequences
It seems like you already understand how to extract the audio stream from a video, so we'll skip that step.
Let's assume you have a video file named video.mp4 and an audio file named audio.mp3. You want to take stills from the video and create a slideshow with audio.mp3 as the audio track.
First we'll extract the stills. fps=1/10 means that frames will be extracted every tenth second. -ss 00:00:5 means that we'll begin five seconds from the start. The first bit of a video is often a fade-in containing mostly black, so it might be desirable to skip that.
ffmpeg -ss 00:00:5 -i video.mp4 -vf fps=1/10 image%03d.png
Next we'll count how many images that resulted in. This will be handy to know when we're specifying the frame rate of the slideshow.
imgcount=$(find . -maxdepth 1 -name "image*" | wc -l)
echo $imgcount
The duration of the audio track would also be nice to know. With MP3 and similar formats, especially when encoded with a variable bit rate, estimation of duration can be fraught. To get around this the audio file can be converted into a WAV file, f.ex, and then the duration estimated.
adur=$(ffprobe -v error -select_streams a:0 -show_entries stream=duration \
-of default=noprint_wrappers=1:nokey=1 audio.mp3)
echo $adur
Now we'll recombine the images into a slideshow. $imgcount/$adur specifies the display rate of the stills so the duration matches that of the audio file reasonably well. -r 25 specifies the actual frame-rate of the video. Not all formats and video players accept unusual frame-rates.
ffmpeg -framerate $imgcount/$adur -i image%03d.png -c:v libx264 -r 25 \
-pix_fmt yuv422p slideshow.mp4
This will mux the audio file and the slideshow file and trim to the duration of the shortest of the two. This works if the container format supports the codec of the audio file, if not either the audio file has to be re-converted, or another container format chosen. I find Matroska (.mkv) very useful. In the case of .avi and .ac3 or .mp4 and .mp3 there should be no problem.
ffmpeg -i slideshow.mp4 -i audio.mp3 -codec copy -shortest slideshow-sound.mp4
I'm using avconv for merging an audio file with a video file. I'm using a script in which the video length will vary from file to file. So, I use -shortest flag to stop audio abruptly at the end of the video. I want to fade out audio alone for 2 seconds at the end of the video. But, I don't know the exact length of a particular video.
avconv -i inputvideo.mp4 -i inputaudio.mp3 -c:v copy -c:a aac -strict experimental -shortest videowithfadedaudio.mp4
What should I do?