Combining an image sequence with audio and make a video - audio

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

Related

FFmpeg output video file much smaller than uncompressed input audio file, using option to preserve original audio quality

I attempt to create a video slideshow from a number of image files and an audio file in 2 steps:
Create a temporary video file from a sequence of image files
Add an audio file to the temporary video file with a delay of 5 seconds
The audio file is an uncompressed stereo wav file, encoded with a sample rate of 44100 Hz and a bit depth of 32 bits, with a size of 40.1 MB. To preserve the lossless quality of the input audio file I use the option -c:a aac -b:a 192k as per Slideshow Wiki. However, the final output video file has a size of only 4.49 MB.
How can the output video file be about 10 times smaller than the input audio file and still preserve the original lossless quality?
My code:
ffmpeg -f concat -i slide-sequence.txt -c:v libx264 -r 30 -filter_complex format=yuv420p temp.mp4
ffmpeg -i temp.mp4 -i audio.wav -af "adelay=5000|5000" -c:v copy -c:a aac -b:a 192k out.mp4
How can the output video file be about 10 times smaller than the input audio file and still preserve the original lossless quality?
It does not. AAC is a lossy format. It uses encoding methods to make it sound good although it is lossy.
There are formats that are both compressed and lossless, such as FLAC. YouTube supports this, so use:
ffmpeg -i temp.mp4 -i audio.wav -af "adelay=5000|5000" -c:v copy -c:a flac out.mkv
Note the change of the output container format from MP4 to Matroska (.mkv). YouTube supports Matroska.

How to make FFmpeg automatically inject mp3 audio tracks in the single cycled muted video?

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.

Use FFMPEG to export audios with gaps filled

I have a MKV file with gaps in the audio. That is, there are gaps in the MKV audio track timestamps. According to "ffmpeg", the AC3 audio length is 802 seconds, but when exporting the audio to WAV, the resulting file length is 801'53 seconds. That is, the "exported" audio is shorter.
Triaging the issue with
ffmpeg -i INPUT.mkv -af ashowinfo -map 0:2 -y -frames:a XXXX -f alaw /dev/null
I can confirm that the length difference is consistent with gaps in the timestamps of the original audio frames. There are a handful of missing audio frames. I guess those are replaced by silence in the player.
The command I use to export the audio is:
ffmpeg -i INPUT.mkv -map 0:1 -ac 2 OUTPUT.wav
My question is: How can I instruct FFMPEG to preserve the gaps in the original audio, zero (silence) filled?. The WAV file duration should be the same than the original AC3 audio.
Given my current workflow, I would rather prefer to not keep the original timestamps in the output file but generate a WAV with (tiny) silences instead. I could consider keeping timestamps if there is no other choice, but this could be quite a pain in my workflow.
Advice? Help?
Thanks a lot in advance!
Use
ffmpeg -i INPUT.mkv -map 0:1 -af aresample=async=1 -ac 2 OUTPUT.wav
The aresample filter will insert silent samples within the gaps.

How can i use ffmpeg to extract audio from an video every 5 seconds

I need to extract audio from live stream. I want to generate an audio file every 5s.
if i use the command
ffmpeg -i ***.flv -c:a aac -fs 128k output.aac
I can only get one file. but i want generate a file every 5s.
if i use the commmand
ffmpeg -i ***.mp4 -vf fps=1/5.0 E:\image%d.jpg
I got an image every 1s.
How can i do the same thing to extract audio.
Use the segment muxer:
ffmpeg -i input -map 0:a -f segment -segment_time 5 output_%03d.aac
Add -c copy and use a compatible output container format if you don't want to re-encode.

FFmpeg concat audio with video or gif looping

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.

Resources