FFMPEG Growing Input files - ffmpeg

I'm having issues with piping the ffmpeg out to the ffmpeg input. I have tried as below
ffmpeg -i "y:\3000012936-TXMHD.mxf" -vcodec copy -acodec copy -f mpegts pipe:1 | ffmpeg -re -i pipe:0 -pix_fmt yuv420p -vsync 1 -map 0:v:0 -map 0:a:0 -c:a aac -c:v libx264 -use_template 1 -use_timeline 1 -init_seg_name "init-stream$RepresentationID$-$Bandwidth$.mp4" -media_seg_name "chunk-stream$RepresentationID$-$Number%05d$.$ext$" -b:v 1500k -b:a 128k -ac 2 -profile:v main -level:v 3.0 -s 1920x1080 -r 25 -vsync passthrough -increment_tc 1 -adaptation_sets "id=0,streams=v id=1,streams=a" -g 100 -keyint_min 100 -seg_duration 5 -frag_duration 5 -dash_segment_type auto -f dash "stream.mpd"
But I'm getting an error:
Conversion failed! av_interleaved_write_frame(): Broken pipe Error
writing trailer of pipe:1: Broken pipe
The input file is MXF, and output is going to be MPEG DASH. The reason to do the piping is because the input file is a growing mxf file. If i do it without piping the ffmpeg just closes before the mxf file is written completely.

Why not let ffmpeg read the file at the same framerate as the source media? If ffmpeg reads at the same speed as the file is written then it will never catch up and close the output.
ffmpeg -re -i "y:\3000012936-TXMHD.mxf" . . . . . . . .
There needs to be enough data on the source before you start ffmpeg. In case of network drives there could be a delay between writing data and that data being available over the network.

Related

Adding multiple audio tracks and subtitles to dash manifest (mpd) with ffmpeg

I'm trying to create a website to stream some videos. For each video, I extract video, audio and subtitles in 3 different folders. It happens that a video has multiple audio tracks and multiple subtitles. I did a lot of research and I don't know how to add all of them in the manifest. Right now, I use this command:
ffmpeg -f webm_dash_manifest \
-i video1.mp4 -f webm_dash_manifest \
-i video2.mp4 -f webm_dash_manifest \
-i audio1.webm -f webm_dash_manifest \
-i audio2.webm -f webm_dash_manifest \
-i subtitles.vtt \
-c copy -map 0 -map 1 -map 2 -map 3 \
-f webm_dash_manifest -adaptation_sets "id=0,streams=v id=1,streams=a" manifest.mpd
My two videos have different resolutions and bitrates, and it works perfectly. But I don't get any subtitles and my two audio tracks are considered like one same audio track which has two different bitrates (just like videos). I think I should have many adaptation_sets, but I don't know how to create them.
How can I create that manifest the right way?
After a few days, I found the solution.
My goal is to convert a video into mpeg-dash content which is really great for streaming.
I will encode video to h264, audio to aac, and subtitles to webvtt.
It's good settings for a large browser compatibility.
vp9 is really nice too but too long to encode for me.
Tools required:
ffmpeg: https://www.ffmpeg.org/download.html
mp4dash & mp4fragment: https://www.bento4.com/downloads/
Let's suppose we have a 1080p video file "video.mkv" with these streams:
0: video stream
1: audio stream, it language
2: audio stream, en langugage
3: subtitle stream, it language
4: subtitle stream, en language
1. Extracting differents streams
1.1 Video
I extract and transcode video stream to differents resolutions and bitrates:
ffmpeg -i video.mkv -an -sn -c:0 libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 5300k -maxrate 5300k -bufsize 2650k -vf 'scale=-1:1080' tmp/video/video-1080.mp4
ffmpeg -i video.mkv -an -sn -c:0 libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 2400k -maxrate 2400k -bufsize 1200k -vf 'scale=-1:720' tmp/video/video-720.mp4
ffmpeg -i video.mkv -an -sn -c:0 libx264 -x264opts 'keyint=24:min-keyint=24:no-scenecut' -b:v 600k -maxrate 600k -bufsize 300k -vf 'scale=-1:360' tmp/video/video-360.mp4
1.2 Audio
ffmpeg -i video.mkv -map 0:1 -ac 2 -ab 192k -vn -sn tmp/audio/audio-it.mp4
ffmpeg -i video.mkv -map 0:2 -ac 2 -ab 192k -vn -sn tmp/audio/audio-en.mp4
1.3 Subtitle
ffmpeg -i video.mkv -map 0:3 -vn -an tmp/subtitle/subtitle-it.vtt
ffmpeg -i video.mkv -map 0:4 -vn -an tmp/subtitle/subtitle-en.vtt
You can use the "-loglevel warning" option to see less informations.
2. Fragment video and audio
2.1 Video
mp4fragment tmp/video/video-1080.mp4 tmp/video/f-video-1080.mp4
mp4fragment tmp/video/video-720.mp4 tmp/video/f-video-720.mp4
mp4fragment tmp/video/video-360.mp4 tmp/video/f-video-360.mp4
2.2 Audio
mp4fragment tmp/audio/audio-it.mp4 tmp/audio/f-audio-it.mp4
mp4fragment tmp/audio/audio-en.mp4 tmp/audio/f-audio-en.mp4
3. Split files and create the dash manifest
mp4dash --mpd-name=manifest.mpd tmp/video/f-video-1080.mp4 tmp/video/f-video-720.mp4 tmp/video/f-video-360.mp4 tmp/audio/f-audio-it.mp4 tmp/audio/f-audio-en.mp4 \[+format=webvtt,+language=it\]tmp/subtitle/subtitle-it.vtt \[+format=webvtt,+language=en\]tmp/subtitle/subtitle-en.vtt
You can now delete the tmp folder
rm -rf tmp
(and your source file if you don't need it anymore)
You have now your mpeg-dash content which can be streamed. You have to serve your files (allow cors and enable byte range request).
I use angular and rx-player as player. I can switch language, subtitles and the video quality is adaptative to the client's bandwidth !
Rx-player: https://github.com/canalplus/rx-player

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.

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

No sound in output clips. RTSP to mp4 ffmpeg

I was using the following command for recording input streams to mp4 into 10 seconds clip. But since few days there is no voice in the output mp4 clips.
Command is
ffmpeg -i rtsp://localhost:8554/mystream -c copy -bsf:a aac_adtstoasc -f segment -segment_time 10 -reset_timestamps 1 -map 0 output%d.mp4
ffmpeg output
I think its because of the format of input stream but i don't know for sure and also, i don't know how to solve it.
I solved it using the following command.
ffmpeg -i rtsp://localhost:8554/mystream -c:a aac -c:v copy -bsf:a aac_adtstoasc -f segment -segment_time 10 -reset_timestamps 1 -map 0 output%d.mp4
changing -c to -c:a aac -c:v

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