ffmpeg raspberry raspbian rapivid streaming - ffmpeg

I am now playing with raspivid on Raspbian and a raspberry pi equipped with a PinoIR camera module.
I am almost done with the setup and have found a pre-compiled version of FFmpeg 3.1.1 to experiment a streaming to a youtube live stream by means of the command:
raspivid -o - -t 0 -vf -hf -fps 30 -b 6000000 | ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/<SESSION>
Is there any parameter I can use to also stream to a local machine (e.g. through a VLC client reading the stream) ?
I have achieved to do it in another bash with cvlc:
cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}':demux=h264
but executing both bashes at the same time is not possible as the input camera is locked by the system.
So, I looked in the ffmpeg documentation and found an interesting thread on multiple outputs : https://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs.
Then, I have tried the piped processes and another ffmpeg call to the inital command:
raspivid -o - -t 0 -vf -hf -fps 30 -b 6000000 | ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/<SESSION> | ffmpeg -f h264 -i - -vcodec copy -f rtsp -rtsp_transport tcp rtsp://localhost:8888/live.sdp
It appears to have a syntax error, and maybe it is not the best way of achieving this. Could you please put me on the right track ?
Thanks and have a nice night !
Nicolas

Related

How to overlay a png to piped video source with audio mixed in via ffmpeg?

I'm successfully streaming silent video with music added from my Raspberry Pi (Raspbian) to YouTube via ffmpeg, with the help of this GitHub gist and this post:
raspivid -o - -t 0 -vf -hf -w 1280 -h 720 -fps 25 -b 4000000 | \
ffmpeg -i music.wav \
-f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental \
-f flv rtmp://a.rtmp.youtube.com/live2/STREAMKEY
The last step of my project to add a transparent, full width/height png overlay to the video (1280x720 size in my case). I've seen a few related answers such as this one and this one.
With the added complexity of piping in a camera feed, mixing in an audio source and outputting to a video stream, I haven't succeeded in adding the image overlay. Where/how would I add a transparent image overlay in the example above?
The ffmpeg part will be
ffmpeg -i music.wav \
-f h264 -i - -i overlay.png
-filter_complex "[1][2]overlay"
-vcodec libx264 -preset ultrafast -tune zerolatency -acodec aac -ab 128k -g 50 -strict experimental \
-f flv rtmp://a.rtmp.youtube.com/live2/STREAMKEY
Since you're altering the video contents, copy can't be used, and the video has to be re-encoded.

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

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.

BMDCapture (bmdtools) capture with Ultrastudio Mini recorder results is color problems

I'm trying to get a Blackmagic Ultrastudio Mini Recorder to stream via avconv to HLS. To test, it's hooked up to an AppleTV and this is the command I'm using:
./bmdcapture -m 14 -C 0 -F nut -f pipe:1 | avconv -vsync passthrough -y -i - -vcodec copy -pix_fmt yuyv422 -strict experimental -f hls -hls_list_size 999 +live -strict experimental out.m3u8
However, the colors are all messed up; suggesting the color format is set incorrectly. The input format is 1280x720 # 59.94 FPS (which is correct) and I've set the format to yuyv422 (though nothing else I've set this to has fixed the error).
Got it!
The Mini Recorder shoots at 10 bits rather than 8 (which I assumed considering Adobe's live encoder said it would be 8).
Here is the fixed code:
./bmdcapture -m 14 -p yuv10 -C 0 -F nut -f pipe:1 | avconv -vsync passthrough -y -i - -vcodec copy -pix_fmt uyvy422 -strict experimental -f hls -hls_list_size 999 +live -strict experimental out.m3u8

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.

ffmpeg, conversion to flv: how to get better quality?

I am using ffmpeg to convert any avi/wmv videos to flv.
My trouble is that the flv result is quite poor: it gives me big pixelitaed boxes.
I tried to use some -b parameters with no good results:
ffmpeg -i 1268459654.wmv -ar 22050 -ab 32 -f flv -s 640x480 x.flv
ffmpeg -i 1268459654.wmv -ar 22050 -ab 32 -f flv -s 640x480 -b 500k x.f4v
I also tried
ffmpeg -i 1268459654.wmv -vcodec libx264 -s 360x240 x.mp4
ans got: "Unknown encoder 'libx264'"
Any solution for that ?
libx264 does not come pre-installed (licensing issues I believe) if you've downloaded it via yum/RPM. You'll need to download the source and compile it yourself and specify libx264. Here's a command line I've used in the past with decent results, and I would consider the MP4 Container over the dated, FLV format personally.
ffmpeg -i (file) -acodec libfaac -ab 44k -vcodec libx264 -vpre normal -crf 30 -threads 0
Make note of the "-vpre normal", as you should have some presets available under:
/usr/share/ffmpeg/libx264-normal.ffpreset or similar.
More details on compiling from source.

Resources