FFmpeg simple 1:1 overlay and concatenate? - ffmpeg

I am using ffmpeg on Ubuntu 14.04 (Jon Severinsson's PPA) and am playing video files out of a folder - one by one.
First question I wasn't able to figure out yet - how can I add a simple overlay - 720p footage with 720p overlay (with partial transparency)? So there is no resize or alignment needed - just the 1:1 overlay. I tried a lot already with -vf and -filter_complex but didn't show up.
Second question - with concatenate, is it possible to have the switches between the files seamless? Best without creating a new file - so, on the fly? I need to reduce the gaps between the file switches or eliminate them completely.
This is my bash right now:
#!/usr/bin/env bash
while :; do
files=(*)
ffmpeg -re -i "${files[$RANDOM % ${#files[#]}]}" -acodec copy -vcodec copy -f flv ServerAddress
done
So I have everything in /vod - the videofiles, as well as the overlay.png
Thanks a bunch in advance,
Tim

For the overlay you need to scale the image to the original source dimensions.
To concat multiple source files that have the same codec use the concat demuxer.
Eg:
Make a playlist.txt with the following format:
file '/path/to/file_1'
file '/path/to/file_2'
file '/path/to/file_3'
[..]
And then:
ffmpeg -f concat -i playlist.txt -i overlay.png -filter_complex "[1:v] scale=1280:720 [ovr];[0:v][ovr] overlay=0:0" ...
If the video and the image are the same size you can just use:
ffmpeg -f concat -i playlist.txt -i overlay.png -filter_complex "[0:v] overlay"
Update:
Full example:
You cannot filter and copy the video stream at the same time!
ffmpeg -re -f concat -i playlist.txt -i overlay.png -filter_complex "[0:v] overlay" -c:v h264 -c:a libfdk_aac -ar 44100 -f flv rtmp://...
If your audio stream is valid and has one of the supported audio rates (44100, 22050, 11025) you can do:
ffmpeg -re -f concat -i playlist.txt -i overlay.png -filter_complex "[0:v] overlay" -c:v h264 -c:a copy -f flv rtmp://...

Related

Hardcoding subtitles from DVD or VOB file with ffmpeg

I have some DVDs that I would like to encode so that I can play them on a Chromecast, with subtitles. It seems that Chromecast only supports text-based subtitle formats, while DVD subtitles are in a bitmap format, so I need to hardcode the subtitles onto the video stream.
First I use vobcopy to create a VOB file:
vobcopy -I /dev/sr0
Next I want to use ffmpeg to encode it as a video stream in a format that is supported by the Chromecast. This is the closest I've come so far (based on the ffmpeg documentation):
ffmpeg -analyzeduration 100M -probesize 100M -i in.vob \
-filter_complex "[0:v:0][0:s:0]overlay[vid]" -map "[vid]" \
-map 0:3 -codec:v libx264 -crf 20 -codec:a copy out.mkv
The -filter_complex "[0:v:0] [0:s:0]overlay[vid] parameters should overlay the first subtitle stream on the first video stream (-map 0:3 is for the audio). This partially works, but the subtitles are only shown for a fraction of a second (I'm guessing one frame).
How can I make the subtitles display for the correct duration?
I'm using ffmpeg 4.4.1 on Linux, but I've also tried the latest snapshot version, and tried gstreamer and vlc (but didn't get far).
The only solution I found that worked perfectly was a tedious multi-stage process.
Copy the DVD with vobcopy
vobcopy -I /dev/sr0
Extract the subtitles in vobsub format using mencoder. This command will write subs.idx and subs.sub. The idx file can be edited if necessary to tweak the appearance of the subtitles.
mencoder *.vob -nosound -ovc frameno -o /dev/null \
-vobsuboutindex 0 -sid 0 -vobsubout subs
Copy the audio and video from the VOB into an mkv file. ffprobe can be used to identify the relevant video and audio stream numbers.
ffmpeg -fflags genpts -i *vob -map 0:1 -map 0:3 \
-codec:v copy -codec:a copy copied_av.mkv
Merge the subtitles with the audio/video stream.
mkvmerge -o merged.mkv copied_av.mkv subs.sub subs.idx
Then ffmpeg will work reliably with the mkv file to write hardcoded subtitles to the video stream.
ffmpeg -i merged.mkv -filter_complex "[0:v:0][0:s:0]overlay[vid]" \
-map [vid] -map 0:1 -codec:v libx264 -codec:a copy hardcoded.mkv

How to add multiple audio files at specific times, on a silence audio file using ffmpeg?

I need to overlay audio files at specific times, on an existing silence.mp3. Something like that:
[----[...audio1...]----------[...audio2...]---------------]
I've tried the following but it doesn't work:
ffmpeg -y -i silence.mp3 -itsoffset 4 -i audio1.mp3 -itsoffset 30 -i audio2.mp3 -c:a copy final.mp3
Any help would be appriciated. Thank you.
There are several methods.
adelay, amix
Use the adelay and amix filters:
ffmpeg -i audio1.mp3 -i audio2.mp3 -filter_complex "[0]adelay=4s:all=1[0a];[1]adelay=30s:all=1[1a];[0a][1a]amix=inputs=2[a]" -map "[a]" output.mp3
Note that the amix filter will reduce volume of the output to prevent clipping. Followup with dynaudnorm or volume filters if desired.
adelay, concat filter
Or adelay and concat filters. This assumes audio1.mp4 is 10 seconds long, and both inputs have the same sample rate and channel layout:
ffmpeg -i audio1.mp3 -i audio2.mp3 -filter_complex "[0]adelay=4s:all=1[0a];[1]adelay=16s:all=1[1a];[0a][1a]concat=n=2:v=0:a=1[a]" -map "[a]" output.mp3
anullsrc, concat demuxer
Or generate silent files as spacers with the anullsrc filter:
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 4 4.mp3
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 16 16.mp3
Create input.txt:
file '4.mp3'
file 'audio1.mp3'
file '16.mp3'
file 'audio2.mp3'
Then use the concat demuxer:
ffmpeg -f concat -i input.txt -c copy output.mp3

Is there a way to pipe input video into ffmpeg?

ffmpeg -f avfoundation -i "1:0" -vf "crop=1920:1080:0:0" -pix_fmt yuv420p -y -r 30 -c:a aac -b:a 128k -f flv rtmp://RTMP_SERVER:RTMP_PORT/STREAM_KEY
Hello guys, the above command works pretty well. It records the audio/video of the computer. But what I want to do is pipe a repeating video or image(png/jpeg/gif), so that there is no live video feed from the computer, but just the image on the stream with the audio.
How would you go about doing this?
Also, if you know any programming interfaces that can do this same thing, please give suggestions. Because I would rather not use a CLI.
I think you should be able to achieve this by using -loop and some -map:ing. I can't test with avfoundation myself but something like this works for me:
ffmpeg -loop 1 -i image.png -i file_to_take_audio_from.mp4 -vf "scale=1920:1080:0:0" -pix_fmt yuv420p -r 30 -c:a aac -b:a 128k -map 0:v -map 1:a output.mp4
Replace -i file_to_take_audio_from.mp4 with -f avfoundation -i "1:0" and output.mp4 with -f flv rtmp://RTMP_SERVER:RTMP_PORT/STREAM_KEY.
Also you might be able to skip -vf if the image has correct resolution.
Hope that helps!
Use none or no value at all (:0) for the video device index and provide a secondary input:
ffmpeg -f avfoundation -i :0 -i image.png ...
There's a loop option for images such as animated GIFs and -stream_loop for input streams.
You can use the FFmpeg APIs directly instead of CLI.

Create muted video and black screen video with FFmpeg

I'm trying to use FFmpeg to generate the following from a local mp4 file:
A copy of the original video with no audio
A copy of the original video with audio but without visuals (a black screen instead). This file also needs to be in mp4 format.
After reading through the documentation I am struggling to get the terminal commands right. To remove the audio I have tried this command without any success:
ffmpeg -i file.mp4 -map 0:0 -map 0:2 -acodec copy -vcodec copy
Could anyone guide me towards how to accomplish this?
Create black video and silent audio
Use the color and anullsrc filters. Example to make 10 second output, 1280x720, 25 frame rate, stereo audio, 44100 sample rate:
ffmpeg -f lavfi -i color=size=1280x720:rate=25:color=black -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 10 output.mp4
Remove audio
Only keep video:
ffmpeg -i input.mp4 -map 0:v -c copy output.mp4
Keep everything except audio:
ffmpeg -i input.mp4 -map 0 -map -0:a -c copy output.mp4
See FFmpeg Wiki: Map for more info on -map.
Make video black but keep the audio
Using the drawbox filter.
ffmpeg -i input.mp4 -vf drawbox=color=black:t=fill -c:a copy output.mp4
Generate silent audio
See How to add a new audio (not mixing) into a video using ffmpeg? and refer to the anullsrc example.
To remove the audio you can use this:
ffmpeg -i file.mp4 -c copy -an file-nosound.mp4
notice the -an option
-an (output)
Disable audio recording.
To keep audio but "replace" the video with a black screen, you could do this:
ffmpeg -i file.mp4 -i image.png -filter_complex overlay out.mp4
image.png is a black wallpaper that is placed on top of the video, but there should be better ways of full removing the frames, you could either extract the audio and later create a new video with the audio as a background

FFMPEG How to merge several videos to one file with fade effect and add audio?

Need to merge several videos to one and add sound track mp3 file and last video with sound.
Only found this solution:
https://superuser.com/questions/778762/crossfade-between-2-videos-using-ffmpeg
But uses static file length in parameters...
Ok, i can calculate all lengths before and add audio:
ffmpeg -i music.mp3 -i 1.mov -i 2.mov -f lavfi -i color=black:s=1280x720 -filter_complex "[1:v]format=pix_fmts=yuva420p,fade=t=out:st=4:d=1:alpha=1,setpts=PTS-STARTPTS[va1]; [2:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+4/TB[va2]; [3:v]scale=1280x720,trim=duration=9[over]; [over][va1]overlay[over1]; [over1][va2]overlay=format=yuv420[outv]" -vcodec libx264 -map 0:a -map [outv] -shortest out.mp4
How to merge more than two videos??
And how to add another end.avi which contains sound and mute music.mp3 ?
Using melt, simply and works as a charm:
melt *.MOV -mixer luma -mix 25 -track tmp.mp3 -consumer avformat:out.avi vcodec=libx264 b=5000k
But first need to cut mp3 file to overal video length, but that's not a problem:
ffmpeg -t LENGTH -i music.mp3 -acodec copy tmp.mp3

Resources