Sync audio in video via ffmpeg? - unix

I need to fit audio with video, but sync option is negative (audio is -350 ms) How to fit it with video? I see only examples when audio is +, but not -

ffmpeg -i "sample.mp4" -itsoffset 13.84 -i "sample.mp4" -map 1:v -map 0:a -c copy "movie-video-delayed.mp4"

Related

FFMpeg - Print Date and Time during recording like surveillance

I need to record video from RaspberryPi, using this Bash script:
#!/bin/sh
/usr/bin/ffmpeg -f video4linux2 -input_format h264 -video_size 320x240 -framerate 15 -i /dev/video0 -vcodec copy -an "/var/ayron/videotrap/videos/pctrace_$(date +"%Y_%m_%d_%H_%M_%S").h264"
In this way, I can report the date and time of start recording. But I need to show on video the Date and Time during registration. Which kind of filter must I use?
Thanks for your supply.
Use drawtext filter:
/usr/bin/ffmpeg -f video4linux2 -input_format h264 -video_size 320x240 -framerate 15 -i /dev/video0 -an -vf "drawtext=text='%{localtime\:%Y_%m_%d_%H_%M_%S}'" "/var/ayron/videotrap/videos/pctrace_$(date +"%Y_%m_%d_%H_%M_%S").h264"
You can't filter and stream copy the video at the same time so -vcodec copy has been omitted.
If you want to use colons (:) in the time then you'll have to do some ugly escaping as shown in How to drawtext colon with localtime in ffmpeg -filter_complex?

ffmpeg and nginx - restream with audio reencoding

I use nginx and ffmpeg to restream video from my provider. Previously I use ffmpeg with arguments where I reencoding video and reencoding audio, because my server is to slow I resigned from reencoding.
So now, I use that command :
ffmpeg -re -i http://link.somelink.com:6565/21d12d1/17233 -map 0 -c copy -bsf:a aac_adtstoasc -f flv -flvflags no_duration_filesize rtmp://test_ip/canal/stream
This works only when my provider streaming with aac audio codec, but sometimes my provider change audio codec to ac3. And then this doesn't work. I try something like this :
ffmpeg -thread_queue_size 32768 -re -i http://link.somelink.com:6565/21d12d1/17233 -c:v copy -c:a aac -f flv -flvflags no_duration_filesize rtmp://test_ip/canal/stream
And it all looks like it's all right in console with ffmpeg, but my restreaming video doesn't work. Ngnix throws 304 exception sometime.
Any suggestions?
Please help,
It's very important for me...
Ac3 is not in supported codecs list. You should encode your stream accordingly.
RTMP supports only a limited number of codecs. The most popular RTMP video codecs are H264, Sorenson-H263 (aka flv) and audio codecs AAC, MP3, Nellymoser, Speex. If your video is encoded with these codecs (the most common pair is H264/AAC) then you do not need any conversion. Otherwise you need to convert video to one of supported codecs.
https://github.com/arut/nginx-rtmp-module/wiki/Getting-started-with-nginx-rtmp

Mixing audio stream into video stream using ffmpeg while retaining original audio from the video stream as background [duplicate]

Can I overlay/downmix two audio mp3 files into one mp3 output file using ffmpeg?
stereo + stereo → stereo
Normal downmix
Use the amix filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amix=inputs=2:duration=longest output.mp3
Or the amerge filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amerge=inputs=2 -ac 2 output.mp3
Downmix each input into specific output channel
Use the amerge and pan filters:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex "amerge=inputs=2,pan=stereo|c0<c0+c1|c1<c2+c3" output.mp3
mono + mono → stereo
Use the join filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex join=inputs=2:channel_layout=stereo output.mp3
Or amerge:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amerge=inputs=2 output.mp3
mono + mono → mono
Use the amix filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amix=inputs=2:duration=longest output.mp3
More info and examples
See FFmpeg Wiki: Audio Channels
Check this out:
ffmpeg -y -i ad_sound/whistle.mp3 -i ad_sound/4s.wav -filter_complex "[0:0][1:0] amix=inputs=2:duration=longest" -c:a libmp3lame ad_sound/outputnow.mp3
I think it will help.
The amix filter helps to mix multiple audio inputs into a single output.
If you run the following command:
ffmpeg -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex amix=inputs=3:duration=first:dropout_transition=3 OUTPUT
This command will mix 3 input audio streams (I used two mp3 files, in the example below) into a single output with the same duration as the first input and a dropout transition time of 3 seconds.
The amix filter accepts the following parameters:
inputs:
The number of inputs. If unspecified, it defaults to 2.
duration:
How to determine the end-of-stream.
longest:
The duration of the longest input. (default)
shortest:
The duration of the shortest input.
first:
The duration of the first input.
dropout_transition:
The transition time, in seconds, for volume renormalization when an input stream ends. The default value is 2 seconds.
For example, I ran the following command in Ubuntu:
FFMPEG version: 3.2.1-1
UBUNTU 16.04.1
ffmpeg -i background.mp3 -i bSound.mp3 -filter_complex amix=inputs=2:duration=first:dropout_transition=0 -codec:a libmp3lame -q:a 0 OUTPUT.mp3
-codec:a libmp3lame -q:a 0 was used to set a variable bit rate. Remember that, you need to install the libmp3lame library, if is necessary. But, it will work even without the -codec:a libmp3lame -q:a 0 part.
Reference: https://ffmpeg.org/ffmpeg-filters.html#amix
For merging two audio files with different volumes and different duration following command will work:
ffmpeg -y -i audio1.mp3 -i audio2.mp3 -filter_complex "[0:0]volume=0.09[a];[1:0]volume=1.8[b];[a][b]amix=inputs=2:duration=longest" -c:a libmp3lame output.mp3
Here duration can be change to longest or to shortest, you can also change the volume levels according to your need.
If you're looking to add background music to some voice use the following command as in the gaps the music will become loud automatically:
ffmpeg -i bgmusic.mp3 -i audio.mp3 -filter_complex "[1:a]asplit=2[sc][mix];[0:a][sc]sidechaincompress=threshold=0.003:ratio=20[bg]; [bg][mix]amerge[final]" -map [final] final.mp3
In this threshold is something whose value will decide how much loud the audio should be, the less the threshold more the audio will be. Ratio gives how much the other audio should be compressed, the more the ratio the more the compression is.
If they are different length, you can use apad to add a silent sound to the shortest one
With Bash
set 'amovie=a.mp3 [gg]; amovie=b.mp3 [hh]; [gg][hh] amerge'
ffmpeg -f lavfi -i "$1" -q 0 c.mp3
Example
You can use the following command arguments:
// Command is here
let commandValue = "-y -i \(recordedAudioPath) -i \(backgroundAudio) -filter_complex [\(0):a][\(1):a]amerge=inputs=\(2)[a] -map [a] -ac \(2) -shortest -preset ultrafast \(outputPath)"
MobileFFmpeg.execute(commandValue)

Sometimes ffmpeg crashes and the process still on task manager in windows

I'm using ffmpeg for transcoding my videos on my servers.
Some times ffmpeg carshes in transode during, and that process stalled on task manager and it used ram,but not consumed cpu(it seems ffmpeg crashed).
1- Is there any solution for knowing that how can I manage this process on my servers?
2- Must be handle that from web server side or can I manage it from ffmpeg side?
I mean, ffmpeg has there any property for when transcoding take a long time,that stops.
Or Can I handle this from server side(iis recycling stops the process in run time transcoding, i don't want to use that, maybe I'm wrong, just help me).
What is the best solution ?
I used this code for example:
ffmpeg -i kata.mp4 -filter_complex
[0:v]drawtext=fontfile=OpenSansRegular.ttf:text=localhost/Parsa:fontcolor
=white:r=25:box=1:boxcolor=black#0.3:boxborderw=3:fontsize=15:x=15:y=(h-text_h-
15)[v];[v]split=4[s0][s1][s2][s3];[s0]scale=hd720[v0];[s1]scale=hd480[v1];
[s2]scale=nhd[v2];[s3]scale=cga[v3] -map [v0] -map [v1] -map [v2] -map [v3] -map
a? -c:v libx264 -c:a aac -f tee -g 48 -threads 0 "
[select='v\:0,a':f=hls:hls_list_size=0]../video/720p/out.m3u8|
[select='v\:1,a':f=hls:hls_list_size=0]../video/480p/out.m3u8|
[select='v\:2,a':f=hls:hls_list_size=0]../video/360p/out.m3u8|
[select='v\:3,a':f=hls:hls_list_size=0]../video/200p/out.m3u8"

How do I create playlists/different bandwidth wit ffmpeg?

I am trying to create a hls stream out of an .mp4 file. So far I can create a manifest + .ts files, but I don't have a playlist.m3u8 to deside which manfest I should give the users based on their bandwith. How do I do that?
Here is my current command which creates HLS streams (no playlist):
ffmpeg -i test.mp4 -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_list out.m3u8 -segment_time 10 out%03d.ts
What this creates is out.m3u8:
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10,
out.ts
#EXTINF:10,
out.ts
What I want to create:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=860000
low.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=512000
medium.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=160000
high.m3u8
To do adaptive streaming with HLS first of all you need to encode your video at the bitrates you want to support. Take a look at Apple's encoding recommendations for some examples.
Once you've done that, you then need to segment each video and generate a playlist for it. The final step is to create a master playlist where you add the URLs of the variant playlists and information about each stream, such as the bandwidth, resolution, and so on - this is the playlist you will use as the video source for the player.
For example, let's assume that your source video was shot in 1080p and you want to generate a 360p variant with a video bitrate of 1200k. You could something like that with the following ffmpeg command:
ffmpeg -i 1080p.mov -c:v libx264 -vprofile baseline -vlevel 3.1 -s 640x360 -b:v 1200k -strict -2 -c:a aac -ar 44100 -ac 2 -b:a 96k 360p.mov
Note that the (source) video you generate the variants from needs to be high quality - you can't encode a 1080p video from a 720p one (without upscaling).
Next, run the command similar to the one in your question to generate the playlist and the segments for this video:
ffmpeg -i 360p.mov -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_time 10 -segment_format mpegts -segment_list 360p/playlist.m3u8 -segment_list_type m3u8 360p/fileSequence%d.ts
Now create a master playlist and add the (relative) URL of the playlist you just created. So something like this:
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=1228800,CODECS="mp4a.40.2,avc1.4d401e",RESOLUTION=640x360
360p/playlist.m3u8
(The bandwidth attribute should also take into account the bitrate of the audio, which I haven't done here.)
Repeat the process for the other variants.
The player will use the information about the available streams in the playlist, and the available bandwidth at the time, to determine which stream is the most appropriate to play.

Resources