FFMPEG Audio and Video Streams merging and restreaming - ffmpeg

I have multiple streaming sources that need to combined and re-streamed as a single source.
My sources are:
A local low rate RTP audio stream
A camera
I need to redistribute the combined stream (via UDP multicast) across the local network.
The problem I am seeing is that periodically ffmpeg appears to lock up and stop processing the combination after an indeterminate amount of time (sometimes as little as 15 min sometimes almost an hour). However if I redirect the streams independently (audio or video only) there appears to be no problem and the run indefinitely.
Command
ffmpeg -f rtp -i rtp://127.0.0.1:6666 -f video4linux2 -standard NTSC -s 704x480 -i /dev/video1 -strict experimental -vcodec libx264 -acodec ac3 -preset ultrafast -r 3 -g 3 -keyint_min 6 -x264opts "keyint=6:min-keyint=6:no-scenecut" -b:v 200k -ac 1 -b:a 64k -f mpegts udp://225.1.1.15:30000
Output
ffmpeg version 2.5.1- http://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2014 the FFmpeg developers
built on Dec 18 2014 09:06:26 with gcc 4.8 (Debian 4.8.3-19)
configuration: --enable-gpl --enable-version3 --disable-shared --disable- debug --enable-runtime-cpudetect --enable-libmp3lame --enable-libx264 --enable- libx265 --enable-libwebp --enable-libspeex --enable-libvorbis --enable-libvpx -- enable-libfreetype --enable-fontconfig --enable-libxvid --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvo-aacenc -- enable-libvo-amrwbenc --enable-gray --enable-libopenjpeg --enable-libopus -- disable-ffserver --enable-libass --enable-gnutls --cc=gcc-4.8
libavutil 54. 15.100 / 54. 15.100
libavcodec 56. 13.100 / 56. 13.100
libavformat 56. 15.102 / 56. 15.102
libavdevice 56. 3.100 / 56. 3.100
libavfilter 5. 2.103 / 5. 2.103
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
[rtp # 0xb61abe0] Guessing on RTP content - if not received properly you need an SDP file describing it
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, rtp, from 'rtp://127.0.0.1:6666':
Duration: N/A, start: 0.000000, bitrate: 64 kb/s
Stream #0:0: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s
Input #1, video4linux2,v4l2, from '/dev/video1':
Duration: N/A, start: 1424887596.039777, bitrate: 162039 kb/s
Stream #1:0: Video: rawvideo (YUY2 / 0x32595559`enter code here`), yuyv422, 704x480, 162039 kb/s, 29.97 fps, 29.97 tbr, 1000k tbn, 1000k tbc
No pixel format specified, yuv422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 # 0xb61f900] using cpu capabilities: MMX2 SSE2Fast SSSE3 Cache64
[libx264 # 0xb61f900] profile High 4:2:2, level 2.2, 4:2:2 8-bit
Output #0, mpegts, to 'udp://225.1.1.15:30000':
Metadata:
encoder : Lavf56.15.102
Stream #0:0: Video: h264 (libx264), yuv422p, 704x480, q=-1--1, 200 kb/s, 3 fps, 90k tbn, 3 tbc
Metadata:
encoder : Lavc56.13.100 libx264
Stream #0:1: Audio: ac3, 8000 Hz, mono, fltp, 64 kb/s
Metadata:
encoder : Lavc56.13.100 ac3
Stream mapping:
Stream #1:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Stream #0:0 -> #0:1 (pcm_mulaw (native) -> ac3 (native))
Press [q] to stop, [?] for help
frame= 5 fps=0.0 q=12.0 size= 0kB time=00:00:00.33 bitrate= 0.0kbits/s dup=0 drop=12

Turns out the only way I was able to get the streams to run for an extended period was to split them into two independent streams.

Related

ffmpeg demux into audio and video resets PTS

Demuxing
I am demuxing TS segments into audio and video as follows.
ffmpeg -y -i input.ts -vcodec copy -an output_video.ts
ffmpeg -y -i input.ts -acodec copy -vn output_audio.aac
Inspecting Input
The start_pts and start_time on input.ts are as shown below. I was able to inspect these values using ffprobe -show_streams -print_format json input.ts
"start_pts": 8306558438,
"start_time": "92295.093756",
Inspecting output video
The output .ts has some default start_pts and start_time values as shown below. These were also obtained using the same ffprobe command as indicated above.
"start_pts": 126000,
"start_time": "1.400000",
Inspecting output audio
The same ffprobe command on output_audio.aac shows that the output aac has invalid codec_tag and codec_tag_string as shown below. The start_pts and start_time are not present in the output_audio.aac.
"codec_tag_string": "[0][0][0][0]", (should have been [15][0][0][0])
"codec_tag": "0x0000", (should have been 0xf000)
Questions
Wondering if this difference in the start_pts, start_time, codec_tag is expected?
If it is expected, what can I do to ensure that the all of these parameters get retained on the output?
If it is not expected, is there some more information I can share to track this down?
Note
There were other outputs that I found inconsistent in the ffprobe command for the output_audio.aac like duration etc.. I shared what I thought are most valuable at this point. If required I can share complete outputs from all of the above executions.
[EDIT 07/30/2018 - 08:00 MST]
logs for ffmpeg -y -i input.ts -vcodec copy -an output_video.ts -acodec copy -vn output_audio.aacare as shown below.
ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers
built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-ffplay --enable-frei0r --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.3.0/include/openjpeg-2.3 --enable-nonfree
libavutil 56. 14.100 / 56. 14.100
libavcodec 58. 18.100 / 58. 18.100
libavformat 58. 12.100 / 58. 12.100
libavdevice 58. 3.100 / 58. 3.100
libavfilter 7. 16.100 / 7. 16.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 1.100 / 5. 1.100
libswresample 3. 1.100 / 3. 1.100
libpostproc 55. 1.100 / 55. 1.100
[mpegts # 0x7f88ed803000] start time for stream 0 is not set in estimate_timings_from_pts
Input #0, mpegts, from 'i7h9456s_media_46185.ts':
Duration: 00:00:06.05, start: 86216.852667, bitrate: 2898 kb/s
Program 1
Stream #0:0[0x102]: Data: timed_id3 (ID3 / 0x20334449)
Stream #0:1[0x100]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p(tv, smpte170m, progressive), 640x360 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:2[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 190 kb/s
Output #0, mpegts, to '../output_video.ts':
Metadata:
encoder : Lavf58.12.100
Stream #0:0: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p(tv, smpte170m, progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 29.97 fps, 29.97 tbr, 90k tbn, 90k tbc
Output #1, adts, to '../output_audio.aac':
Metadata:
encoder : Lavf58.12.100
Stream #1:0: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 190 kb/s
Stream mapping:
Stream #0:1 -> #0:0 (copy)
Stream #0:2 -> #1:0 (copy)
Press [q] to stop, [?] for help
frame= 180 fps=0.0 q=-1.0 Lsize= 2088kB time=00:00:06.03 bitrate=2833.8kbits/s speed= 904x
video:1918kB audio:142kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.349750%
All the differences are expected. FFmpeg removes input timestamp offset unless set otherwise. The MPEG-TS muxer also adds a timestamp offset.
You can preserve source timestamps when remuxing TS, and also avoid the muxer's offset, like this,
ffmpeg -y -copyts -i input.ts -vcodec copy -an -muxdelay 0 -muxpreload 0 output_video.ts
As .aac is a raw ADTS stream, there is no codec tag string present. This is expected. Mux to .m4a or .mka or a similar container if tags are needed.

Capturing audio and video from different sources, how to sync?

Here is my code:
Lapaki:~ Lapaki$ /Users/Lapaki/Desktop/ffmpeg -f avfoundation -video_size 960x540 -pixel_format uyvy422 -framerate ntsc -thread_queue_size 8B -i "XI:none" -f avfoundation -thread_queue_size 8B -i "none:XI" -vf 'crop=iw-240:ih:120:0' -af 'asetpts=PTS+.58735/TB' -pix_fmt yuv420p -aspect 4:3 -s 720x480 -q:v 3 -maxrate 5000k -bufsize 2000k -acodec ac3 -ac 2 -ab 256k -ar 48000 -f dvd /Users/Lapaki/Desktop/FF\ Test/`date +%F`\ `date +%H_%M_%S`.mpg
ffmpeg version 3.2.3-tessus Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-version3 --disable-ffplay --disable-indev=qtkit --disable-indev=x11grab_xcb
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Input #0, avfoundation, from 'XI:none':
Duration: N/A, start: 648413.295900, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 960x540, 29.97 fps, 29.97 tbr, 1000k tbn, 1000k tbc
Input #1, avfoundation, from 'none:XI':
Duration: N/A, start: 648413.884042, bitrate: 3072 kb/s
Stream #1:0: Audio: pcm_f32le, 48000 Hz, stereo, flt, 3072 kb/s
Output #0, dvd, to '/Users/Lapaki/Desktop/FF Test/2017-02-16 04_16_33.mpg':
Metadata:
encoder : Lavf57.56.101
Stream #0:0: Video: mpeg2video (Main), yuv420p, 720x480 [SAR 8:9 DAR 4:3], q=2-31, 200 kb/s, 29.97 fps, 90k tbn, 29.97 tbc
Metadata:
encoder : Lavc57.64.101 mpeg2video
Side data:
cpb: bitrate max/min/avg: 5000000/0/200000 buffer size: 2000000 vbv_delay: -1
Stream #0:1: Audio: ac3, 48000 Hz, stereo, fltp, 256 kb/s
Metadata:
encoder : Lavc57.64.101 ac3
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg2video (native))
Stream #1:0 -> #0:1 (pcm_f32le (native) -> ac3 (native))
Press [q] to stop, [?] for help
[swscaler # 0x7f8e0c8ab400] Warning: data is not aligned! This can lead to a speedloss
frame= 33 fps=0.0 q=3.0 size= 266kB time=00:00:01.06 bitrate=2051.9kbits/sframe= 49 fps= 48 q=3.0 size= 444kB time=00:00:01.54 bitrate=2358.8kbits/sframe= 64 fps= 42 q=3.0 size= 652kB time=00:00:02.08 bitrate=2560.5kbits/sframe= 79 fps= 39 q=3.0 size= 838kB time=00:00:02.59 bitrate=2642.4kbits/sframe= 94 fps= 37 q=3.0 size= 1022kB time=00:00:03.07 bitrate=2720.0kbits/sframe= 109 fps= 36 q=3.0 size= 1208kB time=00:00:03.59 bitrate=2756.5kbits/sframe= 124 fps= 35 q=3.0 size= 1406kB time=00:00:04.07 bitrate=2830.0kbits/sframe= 127 fps= 35 q=3.0 Lsize= 1474kB time=00:00:04.19 bitrate=2876.4kbits/s dup=12 drop=0 speed=1.15x
video:1310kB audio:113kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.604597%
The start of Input #0 is 648413.295900, and the start of Input #1 is 648413.884042.
I've been able to keep the audio and video in very close sync by subtracting the two values (which I assume are wallclock values), and using the asetpts audio filter to delay the audio stream of the recorded mpeg-2 file by that amount.
I'd like to be able to do this exactly though, and that value changes slightly every time I start a new capture. Not to mention, I'd like to be able to do this reliably on different machines, where I assume the value will most likely be different, thus using a calculation as opposed to a fixed number is obviously the best option, if it's possible.
Is there a way to subtract the wallclock start time of input #0 from the wallclock start time of input #1? I'd like to do this inside the asetpts filter, instead of manually finding the difference from a previous run, which again is slightly different every time...
I was thinking something like -af asetpts=PTS-([1:0]RTCSTART-[0:0]RTCSTART)/TB might work, but I have no idea how to format it.
Thanks in advance!

What is the best way to split a transport stream file?

I have a .ts file (Download files here: http://dropcanvas.com/2gmsg/1).
I want to split this video while I expect ALL other properties remain same including pts time.
Here is what I try to achieve this:
ffmpeg -ss 0.000 -i sample.ts -y -c copy -t 3 splitted.ts
Expected start time: 94678.950389
New start time: 1.402367
I expect the above command should only take first 3 seconds of the .ts file and all other stuff to stay same. I've seen copyts and copytb options from the documentation but I wasn't able to use them.
So how do I do this?
Thank you
Here are the logs for copyts. It creates a 0 byte splitted.ts file:
ffmpeg -ss 0:00:00 -i sample.ts -to 00:00:03 -y -c copy -copyts splitted.ts
ffmpeg version 3.0 Copyright (c) 2000-2016 the FFmpeg developers
built with Apple LLVM version 7.0.0 (clang-700.0.72)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.0 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --enable-libfreetype --enable-libvorbis --enable-libvpx --enable-libass --enable-ffplay --enable-libfdk-aac --enable-libopus --enable-libx265 --enable-nonfree --enable-vda
libavutil 55. 17.103 / 55. 17.103
libavcodec 57. 24.102 / 57. 24.102
libavformat 57. 25.100 / 57. 25.100
libavdevice 57. 0.101 / 57. 0.101
libavfilter 6. 31.100 / 6. 31.100
libavresample 3. 0. 0 / 3. 0. 0
libswscale 4. 0.100 / 4. 0.100
libswresample 2. 0.101 / 2. 0.101
libpostproc 54. 0.100 / 54. 0.100
[NULL # 0x7fafac02fc00] start time for stream 2 is not set in estimate_timings_from_pts
Input #0, mpegts, from 'sample.ts':
Duration: 00:00:10.07, start: 94678.950389, bitrate: 934 kb/s
Program 1
Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 854x480 [SAR 1280:1281 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 140 kb/s
Stream #0:2[0x102]: Data: timed_id3 (ID3 / 0x20334449)
Output #0, mpegts, to 'splitted.ts':
Metadata:
encoder : Lavf57.25.100
Stream #0:0: Video: h264 ([27][0][0][0] / 0x001B), yuv420p, 854x480 [SAR 1280:1281 DAR 16:9], q=2-31, 29.97 fps, 29.97 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, 140 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 0 fps=0.0 q=-1.0 Lsize= 0kB time=00:00:00.00 bitrate=N/A speed= 0x
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
This works for me
ffmpeg -seek_timestamp 1 -ss 94678.950389 -i sample.ts -y -c copy -copyts -to 94681.950389 -muxdelay 0 splitted.ts
Your original command can work if you use the frames flag.
ffmpeg -ss 0.000 -i sample.ts -y -c copy -copyts -muxdelay 0 -vframes 90 splitted.ts
Where 90 represents amount of frames in t seconds.

Streams mixed when using -filter_complex amerge in FFmpeg

I am currently having issues with ffmpeg and one of its filters.
I am trying to merge 2 audio streams of a video into one. for this purpose I tried this command:
ffmpeg -i /home/maniaplanet/Videos/ManiaPlanet\ 2014-08-21\ 20-09-13-082.avi.output.mkv -filter_complex "[0:1][0:2] amerge=inputs=2"-c:v copy -c:a libvo_aacenc -b:a 256k /var/www/files/output.mp4
But I get this output:
ffmpeg version 1.0.10 Copyright (c) 2000-2014 the FFmpeg developers
built on Jul 25 2014 07:50:40 with gcc 4.7 (Debian 4.7.2-5)
configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-nonfree --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-libvpx --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-librtmp --enable-avfilter --enable-libfreetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-libvo-amrwbenc --enable-libaacplus --libdir=/usr/lib/x86_64-linux-gnu --disable-vda --enable-libbluray --enable-libcdio --enable-gnutls --enable-frei0r --enable-openssl --enable-libass --enable-libopus --enable-fontconfig --enable-libfdk-aac --enable-libdc1394 --disable-altivec --dis libavutil 51. 73.101 / 51. 73.101
libavcodec 54. 59.100 / 54. 59.100
libavformat 54. 29.104 / 54. 29.104
libavdevice 54. 2.101 / 54. 2.101
libavfilter 3. 17.100 / 3. 17.100
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
libpostproc 52. 0.100 / 52. 0.100
Input #0, matroska,webm, from '/home/maniaplanet/Videos/ManiaPlanet 2014-08-21 20-09-13-082.avi.output.mkv':
Metadata:
ISRC : Video:RGB24 Audio0:Headset (2- Plantronics .Audio 655 DSP) Audio1:Headset (2- Plantronics .Audio 655 DSP)
ENCODER : Lavf55.37.100
Duration: 01:49:48.47, start: 0.000000, bitrate: 3867 kb/s
Stream #0:0: Video: h264 (High), yuv420p, 1280x1024, SAR 1:1 DAR 5:4, 30 fps, 30 tbr, 1k tbn, 60 tbc (default)
Stream #0:1: Audio: mp3, 48000 Hz, stereo, s16, 320 kb/s (default)
Stream #0:2: Audio: mp3, 48000 Hz, stereo, s16, 320 kb/s (default)
File '/var/www/files/output.mp4' already exists. Overwrite ? [y/N] y
Input channel layouts overlap: output layout will be determined by the number of distinct input channels
[libvo_aacenc # 0x7ae800] Unable to set encoding parameters
Output #0, mp4, to '/var/www/files/output.mp4':
Metadata:
ISRC : Video:RGB24 Audio0:Headset (2- Plantronics .Audio 655 DSP) Audio1:Headset (2- Plantronics .Audio 655 DSP)
ENCODER : Lavf55.37.100
Stream #0:0: Audio: aac, 48000 Hz, 4.0, s16, 256 kb/s
Stream #0:1: Video: h264, yuv420p, 1280x1024 [SAR 1:1 DAR 5:4], q=2-31, 30 fps, 90k tbn, 1k tbc (default)
Stream mapping:
Stream #0:1 (mp3) -> amerge:in0
Stream #0:2 (mp3) -> amerge:in1
amerge -> Stream #0:0 (libvo_aacenc)
Stream #0:0 -> #0:1 (copy)
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
I think the important part is at the bottom:
Stream mapping:
Stream #0:1 (mp3) -> amerge:in0
Stream #0:2 (mp3) -> amerge:in1
amerge -> Stream #0:0 (libvo_aacenc)
Stream #0:0 -> #0:1 (copy)
It maps the video stream as the second stream and the audio gets first. How do i resolve this? -map did not help. (Maybe I just used it wrong)
Example
ffmpeg -i input -filter_complex "[0:a:0][0:a:1] amerge=inputs=2 [a]" \
-map [0:v] -map "[a]" -c:v copy -c:a libfdk_aac -ac 2 -b:a 128k output.mp4
Notes
Your output in your question contained 4 channels of audio, but I'll assume you actually wanted to downmix it to stereo. You can do that with the pan audio filter or with -ac 2 as shown above. See FFmpeg Audio Channel Manipulation: 2 × stereo → stereo for an example using pan.
As you guessed, you can control mapping with -map. The order of the mapping can determine the output of the output mapping.
libfdk_aac is the best AAC encoder supported by ffmpeg, and libvo_aacenc is the worst. I switched to libfdk_aac since your build supports it, and it will allow a lower bitrate and still sound fairly good. See the FFmpeg AAC Encoding Guide.
I changed the filtering input from [0:1] to [0:a:0] which means "first input:audio stream type:first (audio) stream". In this case it maps to the same stream but this allows you to be slightly lazier.
You can add -movflags +faststart if your viewers are going to watch this via progressive download in a browser. It will relocate the moov atom from the end of the file to the beginning to allow playback to begin with less of a wait by the viewer.

ffmpeg: AVI to FLV conversion doubles file size

I convert AVI to FLV with ffmpeg using -sameq parameter (same quality):
ffmpeg -i test.avi -sameq -f flv sameq.flv
The resulting file has the same video and audio quality as the original, but it's more than twice the original file size:
84M sameq.flv
41M test.avi
Why does it happen?
Transcoder output:
ffmpeg version N-34750-g070d2d7, Copyright (c) 2000-2011 the FFmpeg developers
built on Nov 12 2011 11:23:07 with gcc 4.6.1
configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-x11grab
libavutil 51. 24. 1 / 51. 24. 1
libavcodec 53. 33. 0 / 53. 33. 0
libavformat 53. 20. 0 / 53. 20. 0
libavdevice 53. 4. 0 / 53. 4. 0
libavfilter 2. 48. 0 / 2. 48. 0
libswscale 2. 1. 0 / 2. 1. 0
libpostproc 51. 2. 0 / 51. 2. 0
Input #0, avi, from 'test.avi':
Duration: 00:06:30.00, start: 0.000000, bitrate: 866 kb/s
Stream #0:0: Video: mpeg4 (Advanced Real Time Simple Profile) (DIVX / 0x58564944), yuv420p, 400x300 [SAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, mono, s16, 64 kb/s
[buffer # 0xa247ae0] w:400 h:300 pixfmt:yuv420p tb:1/1000000 sar:1/1 sws_param:
Output #0, flv, to 'sameq.flv':
Metadata:
encoder : Lavf53.20.0
Stream #0:0: Video: flv1 ([2][0][0][0] / 0x0002), yuv420p, 400x300 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 1k tbn, 25 tbc
Stream #0:1: Audio: mp3 ([2][0][0][0] / 0x0002), 44100 Hz, mono, s16, 128 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (mpeg4 -> flv)
Stream #0:1 -> #0:1 (mp3 -> libmp3lame)
Press [q] to stop, [?] for help
frame= 9742 fps=255 q=0.0 Lsize= 85074kB time=00:06:30.00 bitrate=1787.0kbits/s
video:79163kB audio:5525kB global headers:0kB muxing overhead 0.455568%
Two thing comes to mind:
Compress a video without audio stream to eliminate the audio portion of this issue. BTW, the audio source is HALF the bitrate of the output, that increases the size a little. Use -ar and -ab switches to control the output.
Check out this article on qscale vs quality using -qscale option. Add in the -b (bitrate) and -s (size) and tweak it to your needs.
When all fails, there are a few switches you can try from the ffmpeg website or try using the new H.264 compression, the two pass option is recommended. Have fun compressing
its because of -sameq. It gives you a good quality but pay the price with a bigger file size.
Can you try adding:
-qcomp 1.0
video quantizer scale compression ( VBR ) (default 0.5). Constant of ratecontrol equation. Recommended range for default rc_eq: 0.0-1.0

Resources