Hey guys!
I'm streaming to twitch.tv using the following code:
Code:
INRES="1920x1080" # input resolution
OUTRES="864x486"
FPS="30" # target FPS
QUAL="fast" # x264 preset
STREAM_KEY="$1"
URL="rtmp://live.justin.tv/app/$STREAM_KEY"
ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 \
-f alsa -ac 2 -i pulse -vcodec libx264 -pix_fmt yuv420p -crf 30 -preset "$QUAL" -s "$OUTRES" \
-acodec libmp3lame -ar 44100 -threads 0 \
-f flv "$URL"
The problem is the audio is out of sync right from the start (sound comes after the video).
I can fix it temporarily using the -itsoffset option of ffmpeg, but after a while it just goes out of sync again.
Tried writing to an mkv file instead of the stream URL, the problem still appeared.
Log:
Code:
$ stream
ffmpeg version 2.0 Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 20 2013 08:30:53 with gcc 4.8 (Debian 4.8.1-10)
configuration: --enable-gpl --enable-libpulse --enable-libx264 --enable-libmp3lame --enable-nonfree --enable-x11grab
libavutil 52. 47.101 / 52. 47.101
libavcodec 55. 37.102 / 55. 37.102
libavformat 55. 19.103 / 55. 19.103
libavdevice 55. 4.100 / 55. 4.100
libavfilter 3. 88.102 / 3. 88.102
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.104 / 0. 17.104
libpostproc 52. 3.100 / 52. 3.100
[x11grab @ 0x233f3e0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1920 height: 1080
[x11grab @ 0x233f3e0] shared memory extension found
Input #0, x11grab, from ':0.0':
Duration: N/A, start: 1382268991.129668, bitrate: 1990656 kb/s
Stream #0:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1920x1080, 1990656 kb/s, 30 tbr, 1000k tbn, 30 tbc
Guessed Channel Layout for Input Stream #1.0 : stereo
Input #1, alsa, from 'pulse':
Duration: N/A, start: 1382268990.754322, bitrate: 1536 kb/s
Stream #1:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
[swscaler @ 0x2321040] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0x2372da0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x2372da0] profile High, level 3.1
[libx264 @ 0x2372da0] 264 - core 133 r2339 585324f - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=1 ref=2 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=6 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=1 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=30 rc=crf mbtree=1 crf=30.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://live.justin.tv/app/live_50378913_TW6NMKbySlLiywXK5Aykr5s74sUNV1':
Metadata:
encoder : Lavf55.19.103
Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 864x486, q=-1--1, 1k tbn, 30 tbc
Stream #0:1: Audio: mp3 (libmp3lame) ([2][0][0][0] / 0x0002), 44100 Hz, stereo, s16p
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo -> libx264)
Stream #1:0 -> #0:1 (pcm_s16le -> libmp3lame)
Press [q] to stop, [?] for help
frame= 45 fps=0.0 q=36.0 size= 3kB time=00:00:00.10 bitrate= 270.9kbits/
frame= 49 fps= 37 q=36.0 size= 30kB time=00:00:00.23 bitrate=1056.1kbits/
frame= 55 fps= 30 q=36.0 size= 48kB time=00:00:00.43 bitrate= 905.1kbits/
frame= 59 fps= 25 q=36.0 size= 85kB time=00:00:00.56 bitrate=1235.1kbits/
frame= 72 fps= 25 q=36.0 size= 116kB time=00:00:01.00 bitrate= 948.0kbits/
.
.
.
Is there a way to fix this?
I'd appreciate any advice, thanks in advance!
edit:
Tried to use aac instead of libmp3lame. At the start of the stream the audio actually came before the video, after a while they were in sync and then it started to come after the video. So it progressively worsened. Same thing happens with libmp3lame, only difference is that using libmp3lame the sound always comes after the video, never before it.
edit2:
Fixed it by removing the -r input option. Figured out (from ffmpeg documentation) it generates timestamps as if frame rate were constant.