Arecord->FFMPEG works but FFMPEG w/ ALSA stutters? - ffmpeg

I am trying to stream audio from my Pi Zero and my I2s MEMS mic. I would like to stream using FFMPEG and ALSA (which I have already compiled) but I'm running into stuttering issues.
FFMPEG + ALSA
~/special/ffmpeg/ffmpeg -report -f alsa -ar 48000 -ac 2 -acodec pcm_s32le -i mic_sv -f lavfi -i testsrc -c:v h264_omx -c:a aac -ab 32k -bufsize 32k -f flv rtmp://209.85.230.23/live2/KEY
This results in constant stuttering and choppiness.
Arecord piped directly to FFMPEG
arecord -Dmic_sv -c2 -r48000 -fS32_LE | ~/special/ffmpeg/ffmpeg -report -acodec pcm_s32le -i - -f lavfi -i testsrc -c:v h264_omx -acodec aac -ab 32k -bufsize 32k -f flv rtmp://209.85.230.23/live2/KEY
This results in a coherent audio stream, but with skipping every 5 seconds or so.
Arecord recorded to a wav file, piped into FFMPEG
arecord -Dmic_sv -c2 -r48000 -fS32_LE -twav temp.v &
~/special/ffmpeg/ffmpeg -report -re -i temp.v -f lavfi -i testsrc -c:v h264_omx -ac 2 -acodec aac -ab 32k -bufsize 32k -async 2 -f flv rtmp://209.85.230.23/live2/KEY
This results in a perfect audio stream.
I don't know why #3 works but #2 and #1 cause problems. Any suggestions?

Well well off we go to my every first Stackoverflow post :)
So you probably experience a message saying [alsa buffer run] or similar.
The reason for this is indeed because the buffer fills up faster than it can be processed. The only real mitigation I found was to change the sampling rate to 22050hz
here is some code that should work:
ffmpeg -re -v verbose -hide_banner -f v4l2 -thread_queue_size 1024 -re -i temp.v -ar 11025 -f alsa -ac 1 -thread_queue_size 1024 -i default -b:a 64k -c:v libx264 -preset ultrafast -strict experimental -f flv -g 20 -keyint_min 20 -maxrate 2M -bufsize 6M -acodec aac rtmp://209.85.230.23/live2/KEY
p.s. Sorry for the late reply.

Related

FFMPEG SCREEN RECORDING: How to get H265 (libx265) recording using ffmpeg with xorg?

I really would appreciate all the help I can get here.
I'm trying to use the libx265 codec for recording an xorg dummy screen. The command that currently works for H264 (libx264 codec) is:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720
-thread_queue_size 4096 -i :0.0+0,0 -f alsa -acodec aac -strict -2 -ar 44100 -b:a 128k -af aresample=async=1 -c:v libx264 -preset fast
-profile:v main -level 3.1 -pix_fmt yuv420p -r 30 -crf 21 -g 60 -tune zerolatency -f mp4 capture.mp4
In trying to get H265 instead, I first changed the codec to libx265 like below:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720
-thread_queue_size 4096 -i :0.0+0,0 -f alsa -acodec aac -strict -2 -ar 44100 -b:a 128k -af aresample=async=1 -c:v libx265 -preset fast
-profile:v main -level 3.1 -pix_fmt yuv420p -r 30 -crf 21 -g 60 -tune zerolatency -f mp4 capture.mp4
But that didn't do it. Although it didn't error, it was producing a file that was playing at twice the recorded speed (i.e. twice the speed of the clip that was recorded).
Then I tried using -x265-params to specify the parameters like this:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720
-thread_queue_size 4096 -i :0.0+0,0 -f alsa -acodec aac -strict -2 -ar 44100 -b:a 128k -af aresample=async=1 -c:v libx264 -preset fast
-x265-params profile=main:level=3.1:crf=21 -pix_fmt yuv420p -r 30 -g 60 -tune zerolatency -f mp4 capture.mp4
And this gave me an error with the following message:
"output file #0 does not contain any stream ffmpeg"
I've tried all sorts of combinations, searched extensively online (for both how to set 265 parameters and on the output file error), but I'm not making a headway. I'm really new to all this. Can anyone please help (with the most simple terms and directions)?

How to reduce audio delay in FFmpeg using HTTP FLV stream?

I have problem with live http flv stream. I'm using the following command and it works great except for the audio. Audio has delay that increasing over time. I can fix the this by removing refresh rate option on the output (-r 30) but then stream latency goes higher for about half second.
ffmpeg -f v4l2 -threads 0 -video_size 672X420 -i /dev/video1 -f alsa -thread_queue_size 512 -i hw:1,0 -c:a aac -ar 44100 -b:a 128k -c:v libx264 -s 672x420 -r 30 -g 60 -preset superfast -tune zerolatency -strict -2 -f flv rtmp://localhost/live/primary
Can someone explain why this happening and what possible fixes are?
On the client I'm using Chrome with flv.js library.
On the server Node-Media-Server.

live streamming using ffmpeg for more than stream on the same time

I'm using this command to stream video in ffmpeg but when I stream more than 3 or 4 streams at the same time interruption happen although the process in my device doesn't exceed 50%
I tried to use ffmpeg lib for each stream but interruption still happened
this is my command :
ffmpeg -re -i test.mp4 -i logo.png -vcodec libx264 -pix_fmt yuv420p -vb 2000000 -g 60
-vprofile main -acodec aac -ab 128000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb
-strict experimental -filter_complex "[0][1]overlay=0:0"
-pass 1 -f mpegts udp://127.0.0.1:1234?pkt_size=1316

FFmpeg Live Stream - Loop Video?

I am trying to stream a video loop to justin.tv using FFmpeg? I have managed to loop an image sequence and combine it with line in audio:
ffmpeg -loop 1 -i imageSequence%04d.jpg -f alsa -ac 2 -ar 22050 -ab 64k \
-i pulse -acodec adpcm_swf -r 10 -vcodec flv \
-f flv rtmp://live.justin.tv/app/<yourStreamKeyHere>
Is it possible to do this with a video file?
Definitely possible. In the recent versions of ffmpeg they have added a -stream_loop flag that allows you to loop the input as many times as required.
The gotcha is that if you don't regenerate the pts from the source, ffmpeg will drop frames after the first loop (as the timestamp will suddenly go back in time). To avoid this, you need to tell ffmpeg to generate the pts so you get an increasing timestamp between loops. This is done with the +genpts call (it has to be before the -i arg).
Here's an example ffmpeg call (replace $F with your input file). This example generates two output streams and the -stream_loop -1 argument tells ffmpeg to continuously loop the input. The output in this case is for a similar stream broadcast ingest (MetaCDN), adjust accordingly to your requirements.
ffmpeg -threads 2 -re -fflags +genpts -stream_loop -1 -i $F \
-s 640x360 -ac 2 -f flv -vcodec libx264 -profile:v baseline -b:v 600k -maxrate 600k -bufsize 600k -r 24 -ar 44100 -g 48 -c:a libfdk_aac -b:a 64k "rtmp://publish.live.metacdn.com/2050C7/dfsdfsd/lowquality_664?hello&adbe-live-event=lowquality_" \
-s 1920x1080 -ac 2 -f flv -vcodec libx264 -profile:v baseline -b:v 2000k -maxrate 2000k -bufsize 2000k -r 24 -ar 44100 -g 48 -c:a libfdk_aac -b:a 64k "rtmp://publish.live.metacdn.com/2050C7/dfsdfsd/highquality_2064?mate&adbe-live-event=highquality_"
Sinclair Media has found a solution by using the lavfi filter and appending :loop=0 to the file name :
This is untested:
ffmpeg -f lavfi -re -i movie=StreamTest.avi:loop=0 \
-acodec libfaac -b:a 64k -pix_fmt yuv420p -vcodec libx264 \
-x264opts level=41 -r 25 -profile:v baseline -b:v 1500k \
-maxrate 2000k -force_key_frames 50 -s 640×360 -map 0 -flags \
-global_header -f segment -segment_list index_1500.m3u8 \
-segment_time 10 -segment_format mpeg_ts \
-segment_list_type m3u8 segmented.ts
But it should create a local "index_1500.m3u8" file that streams the video in "StreamTest.avi".
I just reuse the Rob's answers with a few of modifications in order to provide a file to live streaming
ffmpeg -threads 2 -re -fflags +genpts -stream_loop -1 -i gvf.mp4 -c copy -f mpegts -mpegts_service_id 102 -metadata service_name=My_channel -metadata service_provider=My_Self -max_interleave_delta 0 -use_wallclock_as_timestamps 1 -flush_packets 1 "udp://233.0.0.1:1001?localaddr=10.60.4.237&pkt_size=188"

How to record UDP stream with FFMPEG in chunks?

I'm looking for a way to record a video UDP stream using ffmpeg but in 10mn chunks.
I currently use the following to get 10mn of video (with h264 transcoding).
"ffmpeg -i udp://239.0.77.15:5000 -map 0:0 -map 0:1 -s 640x360 -vcodec libx264 -g 100 -vb 500000 -r 25 -strict experimental -vf yadif -acodec aac -ab 96000 -ac 2 -t 600 -y /media/test.m4 "
My problem is that using command line ffmpeg needs time to resync with the udp stream loosing 2 seconds of video each time. Is it normal ?
Any idea if there is a way to do it in command line or should I tried to use the ffmpeg API ?
Thanks in advance
Ok found it.
Recently ffmpeg add a segmenter, here is the syntax:
-f segment: tell ffmpeg to use the segmenter
-segment_time: chunk size in second
You can use autoincrement file name with something like %03d (000,001,002,003...).
Here is my line to transcode a UDP MPEGTS stream, into H264+AAC and save it to file chunk (60 seconds):
ffmpeg -i udp://239.0.77.15:5000 -map 0:0 -map 0:1 -s 640x360 -vcodec libx264 -g 60 -vb 500000 -strict experimental -vf yadif -acodec aac -ab 96000 -ac 2 -y -f segment -segment_time 60 "xxx-%03d.ts"
This is a better way:
ffmpeg -re -i udp://10.1.1.238:1234?fifo_size=1000000 -vcodec libx264 -vb 500000 -g 60 -vprofile main -acodec aac -ab 128000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -b 1000k -minrate 1000k -maxrate 1000k -strict experimental -f stream_segment -segment_format mpegts -segment_time 5 -segment_atclocktime 1 -reset_timestamps 1 -strftime 1 d:/%H%M%S.mp4
By this code ffmpeg makes series of output files using current system time.

Resources