FFmpeg: how to stream multiple http outputs - http

I'm assigned to search for a solution to stream more than one http output in ffmpeg. I tried the code below which uses pipe, but only the last output works, other ones don't work. The solutions I tried with tee muxer also couldn't work. Any idea how can I do that?
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -f mp3 pipe:1 | ffmpeg -f mp3 -re -i pipe:0 -c copy -listen 1 -f mp3 http://127.0.0.1:4000 -c copy -listen 1 -f mp3 http://127.0.0.1:4010
The code I tried with tee muxer is below:
ffmpeg -re -i sample1.mp3 -i sample2.mp3 -filter_complex amix=inputs=2 -c:a mp3 -f tee "[listen=1:f=mp3]http://127.0.0.1:4000|[listen=1:f=mp3]http://127.0.0.1:4010"
edit: shared the log with code.
ffmpeg -loglevel debug -hide_banner -re -i sample1.mp3 -filter_complex asplit=2 -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4000 -c:a mp3 -listen 1 -f mp3 http://127.0.0.1:4010
Splitting the commandline.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
Reading option '-hide_banner' ... matched as option 'hide_banner' (do not show program banner) with argument '1'.
Reading option '-re' ... matched as option 're' (read input at native frame rate; equivalent to -readrate 1) with argument '1'.
Reading option '-i' ... matched as input url with argument 'sample1.mp3'.
Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument 'asplit=2'.
Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'mp3'.
Reading option '-listen' ... matched as AVOption 'listen' with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'mp3'.
Reading option 'http://127.0.0.1:4000' ... matched as output url.
Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'mp3'.
Reading option '-listen' ... matched as AVOption 'listen' with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'mp3'.
Reading option 'http://127.0.0.1:4010' ... matched as output url.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option loglevel (set logging level) with argument debug.
Applying option hide_banner (do not show program banner) with argument 1.
Applying option filter_complex (create a complex filtergraph) with argument asplit=2.
Successfully parsed a group of options.
Parsing a group of options: input url sample1.mp3.
Applying option re (read input at native frame rate; equivalent to -readrate 1) with argument 1.
Successfully parsed a group of options.
Opening an input file: sample1.mp3.
[NULL # 00000216ab345b80] Opening 'sample1.mp3' for reading
[file # 00000216ab346180] Setting default whitelist 'file,crypto,data'
[mp3 # 00000216ab345b80] Format mp3 probed with size=4096 and score=51
id3v2 ver:4 flags:00 len:134
[mp3 # 00000216ab345b80] pad 576 576
[mp3 # 00000216ab345b80] Skipping 0 bytes of junk at 561.
[mp3 # 00000216ab345b80] Before avformat_find_stream_info() pos: 561 bytes read:32768 seeks:0 nb_streams:1
[mp3 # 00000216ab345b80] demuxer injecting skip 1105 / discard 0
[mp3float # 00000216ab34f3c0] skip 1105 / discard 0 samples due to side data
[mp3float # 00000216ab34f3c0] skip 1105/1152 samples
[mp3 # 00000216ab345b80] All info found
[mp3 # 00000216ab345b80] After avformat_find_stream_info() pos: 22065 bytes read:32768 seeks:0 frames:50
Input #0, mp3, from 'sample1.mp3':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf58.45.100
Duration: 00:12:58.48, start: 0.025057, bitrate: 128 kb/s
Stream #0:0, 50, 1/14112000: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.91
Successfully opened the file.
[Parsed_asplit_0 # 00000216ab34fc80] Setting 'outputs' to value '2'
Parsing a group of options: output url http://127.0.0.1:4000.
Applying option c:a (codec name) with argument mp3.
Applying option f (force format) with argument mp3.
Successfully parsed a group of options.
Opening an output file: http://127.0.0.1:4000.
Matched encoder 'libmp3lame' for codec 'mp3'.
Last message repeated 1 times
[http # 00000216ab44d980] Setting default whitelist 'http,https,tls,rtp,tcp,udp,crypto,httpproxy,data'

FFmpeg can output multiple outputs by default (w/out using the tee muxer):
ffmpeg -re -i sample1.mp3 -i sample2.mp3 \
-filter_complex amix=inputs=2,asplit \
-c:a mp3 -listen 1 f=mp3 http://127.0.0.1:4000 \
-c:a mp3 -listen 1 f=mp3 http://127.0.0.1:4010
Because you need to route the filtergraph output to 2 streams, you need to add asplit filter to your graph, and all you need to do is to have 2 output option sequences in series.
edited: fixed -listen option as pointed out by op

Related

Splitting m4a into multiple files with ffmpeg

I have an audio file (.m4a) and want to split it into smaller pieces using ffmpeg. All answers I have seen do something along one of the following two possibilities:
ffmpeg -i inputFile -map 0 -f segment -segment_time 60 -c copy "$output%03d.m4a
or
ffmpeg -i inputFile -acodec copy -ss start_time -to end_time outputFile
With 1. the first file is fine. From the second file on, I just get a minute of silence in quicktime, and in VLC the file plays but the timing is odd: for example the second file should have second 0 equals to second 60 in the original file. However in vlc it starts playing on second 60 and goes on to second 120.
With 2. I have to set start times and end for each file, unfortunately I notice a small jump when I play one after the other, so it seems as if some miliseconds are lost.
There are definitely a few old questions asked around this, but none of them actually helped me with this.
Quicktime probably doesn't like files starting with a timestamp other than 0.
Use
ffmpeg -i inputFile -map 0 -f segment -segment_time 60 -reset_timestamps 1 -c copy "$output%03d.m4a

FFmpeg strftime microseconds

ffmpeg -i mymp.mp3 -f segment -strftime 1 -segment_time 5 13_REG1_CHANNEL3_%Y-%m-%d_%H-%M-%S-%s.mp3
Is it possible to add at filename microseconds ?
ffmpeg uses strfmttime() to create these filenames. You can use %Y-%m-%d_%H:%M:%S.%fto get as much precision as your system allows. Note the %f at the end.
Update
Your example would be:
ffmpeg -i mymp.mp3 -f segment -strftime 1 -segment_time 5 13_REG1_CHANNEL3_%Y-%m-%d_%H-%M-%S-%f.mp3

Define initial input for zsh read builtin

Bash's builtin read command has a -i option, which specifies an initial input, which the user can accept as it is or edit or add to. I cannot find anything similar for Zsh's read command.
None of the options listed in the zshbuiltins man page seem relevant:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to
the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.
-r Raw mode: a `\' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.
-s Don't echo back characters if reading from the terminal.
-q Read only one character from the terminal and set name to `y' if this character was `y' or `Y' and to `n' otherwise. With this flag set the return status is zero only
if the character was `y' or `Y'. This option may be used with a timeout (see -t); if the read times out, or encounters end of file, status 2 is returned. Input is
read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.
-k [ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the ter-
minal unless one of -u or -p is present. This option may also be used within zle widgets.
Note that despite the mnemonic `key' this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.
-z Read one entry from the editor buffer stack and assign it to the first name, without word splitting. Text is pushed onto the stack with `print -z' or with push-line
from the line editor (see zshzle(1)). This flag is ignored when the -k or -q flags are present.
-e
-E The input read is printed (echoed) to the standard output. If the -e flag is used, no input is assigned to the parameters.
-A The first name is taken as the name of an array and all words are assigned to it.
-c
-l These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the cur-
rent command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.
-n Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word
number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.
-u n Input is read from file descriptor n.
-p Input is read from the coprocess.
-d delim
Input is terminated by the first character of delim instead of by newline.
-t [ num ]
Test if input is available before attempting to read. If num is present, it must begin with a digit and will be evaluated to give a number of seconds, which may be a
floating point number; in this case the read times out if input is not available within this time. If num is not present, it is taken to be zero, so that read returns
immediately if no input is available. If no input is available, return status 1 and do not set any variables.
This option is not available when reading from the editor buffer with -z, when called from within completion with -c or -l, with -q which clears the input queue before
reading, or within zle where other mechanisms should be used to test for input.
Note that read does not attempt to alter the input processing mode. The default mode is canonical input, in which an entire line is read at a time, so usually `read
-t' will not read anything until an entire line has been typed. However, when reading from the terminal with -k input is processed one key at a time; in this case,
only availability of the first character is tested, so that e.g. `read -t -k 2' can still block on the second character. Use two instances of `read -t -k' if this is
not what is wanted.
If the first argument contains a `?', the remainder of this word is used as a prompt on standard error when the shell is interactive.
The value (exit status) of read is 1 when an end-of-file is encountered, or when -c or -l is present and the command is not called from a compctl function, or as described
for -q. Otherwise the value is 0.
The behavior of some combinations of the -k, -p, -q, -u and -z flags is undefined. Presently -q cancels all the others, -p cancels -u, -k cancels -z, and otherwise -z can-
cels both -p and -u.
The -c or -l flags cancel any and all of -kpquz.
How can I achieve the same goal in Zsh?
In zsh you could manually set the variable and use vared instead of read:
name=iconoclast
vared -p "Enter your name: " name
This is the equivalent of the following in bash:
read -p "Enter your name: " -e -i iconoclast name

image -> video -> image not lossless using avconv

I'm trying to test the amount of information lost with some different video codecs. I've got a python script which uses PyPNG to write a series of 8 bit RGB images. I then encode it using avconv, for instance
avconv -r 1 -i ../frames/data%03d.png -c:v ffv1 -qscale:v 0 -r 1
outffv1.avi
I then decode this back into pngs like so
avconv -r 1 -i outffv1.avi -r 1 ./outffv1/frame%03d.png
But when I compare the images before and after the video compression, they are different (mean absolute error of ~~15%). The thing that is confusing me is that this is true (give or take) independent of the codec.
For instance, I get similar answers for libtheora for a range of qscale values.
The png encoding i.e. write to png, and immediately load back in without and video compression step, is lossless.
UPDATE - more precise worked example:
Single input frame here: https://www.dropbox.com/s/2utk1xs2t8heai9/data001.png?dl=0
Encoded to video like this: avconv -r 1 -i ./frames/data%03d.png -c:v ffv1 -qscale:v 0 -r 1 outffv1.avi
resultant video here: https://www.dropbox.com/s/g1babae2a41v914/outffv1.avi?dl=0
decoded to a png again here: https://www.dropbox.com/s/8i8zg1qn7dxsgat/out001.png?dl=0
using this command: avconv -r 1 -i outffv1.avi -qscale:v 31 -r 1 out%03d.png
and image magick differenced like this
compare out001.png ./frames/data001.png diff.png
to give this (non-zero) diff
https://www.dropbox.com/s/vpouk54p0dieqif/diff.png?dl=0
Your video file most likely uses the YUV color format. PNG uses RGB. The conversion of the color is not a lossless process.

Why the Target Duration of the created segments are not the one i specified

I am using the following command
ffmpeg -i Apple.ts -map 0:0 -map 0:1 -c copy -f segment -segment_time 10 -segment_list test.m3u8 -segment_format ts 'fileSequence%d.ts'
The files do get segmented but the values are not precise. see in the .m3u8 generated below the target duration is 12 but i specified 10
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOWCACHE:1
#EXTINF:11.478100,
fileSequence0.ts
#EXTINF:10.410400,
fileSequence1.ts
#EXTINF:11.745067,
fileSequence2.ts
#EXTINF:7.841167,
fileSequence3.ts
#EXTINF:8.024678,
fileSequence4.ts
#EXT-X-TARGETDURATION:12
#EXT-X-ENDLIST
Also if i dont want the floting point duration how do i do it?
To get strict duration you need to ensure your input file has been encoded with a strict I picture rate. Ensure you have set keyframe interval to 10 seconds and sc_threshold to 0 while encoding the original mp4.with ffmpeg. Then run your segmentation command. This will give you exact 10 second segments.
Otherwise ffmpeg will try and cut around the 10 second mark ( or whatever duration you have given ) at the closes I picture it can find.

Resources