Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
mp4 videos audio gradually goes out of sync
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Multimedia
View previous topic :: View next topic  
Author Message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1552
Location: Adelaide/Australia

PostPosted: Tue Aug 21, 2012 9:09 am    Post subject: mp4 videos audio gradually goes out of sync Reply with quote

I have a batch of mp4 video files where the audio starts out in sync but gradually gets further and further out of sync, the further into the mp4 you play, eventually having the sound about 1 second behind the video at the end of the mp4.

I gather it's due to the original recording sound card recording just slightly off the correct frequency and that the audio needs to be re-sampled ever so slightly to bring it in sync.

The output of mplayer shows the file encoding information, as follows :-
Code:
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 48000 Hz, 2 ch, s16le, 112.1 kbit/7.30% (ratio: 14011->192000)
Selected audio codec: [ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))
==========================================================================
AO: [oss] 48000Hz 2ch s16le (2 bytes per sample)
Starting playback...
Movie-Aspect is 1.77:1 - prescaling to correct movie aspect.
VO: [xv] 624x352 => 624x352 Planar YV12


I would prefer some command line or script which I can simply run, which I can leave to re-code the mp4s overnight, rather than having to use a GUI of some form.

Any assistance greatly appreciated.
_________________
...Lyall
Back to top
View user's profile Send private message
xaviermiller
Bodhisattva
Bodhisattva


Joined: 23 Jul 2004
Posts: 8704
Location: ~Brussels - Belgique

PostPosted: Tue Aug 21, 2012 9:12 am    Post subject: Reply with quote

Hello,

Can you check the input and output rates, the difference could come from
- different audio rates (44.1 kHz vs 48 kHz)
- video frame rate (24, 25, 29.5 or 30 fps)

What is your display frame rate ?
_________________
Kind regards,
Xavier Miller
Back to top
View user's profile Send private message
Jaglover
Watchman
Watchman


Joined: 29 May 2005
Posts: 8291
Location: Saint Amant, Acadiana

PostPosted: Tue Aug 21, 2012 9:15 am    Post subject: Reply with quote

I don't think you need to recode, remux should suffice.
_________________
My Gentoo installation notes.
Please learn how to denote units correctly!
Back to top
View user's profile Send private message
aCOSwt
Bodhisattva
Bodhisattva


Joined: 19 Oct 2007
Posts: 2537
Location: Hilbert space

PostPosted: Tue Aug 21, 2012 9:54 am    Post subject: Reply with quote

Stupid question : would the original video track have been in 23.976 fps then converted in 25 forgetting to adjust the sound track ?
If this is possible then you should not do anything to sound track but just reverse the 23.976 to 25 conversion of the video track.
_________________
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1552
Location: Adelaide/Australia

PostPosted: Tue Aug 21, 2012 10:17 am    Post subject: Reply with quote

Does the following help at all?

Code:
$ mplayer -msglevel all=6 Test.mp4
MPlayer SVN-r33094-4.5.3 (C) 2000-2011 MPlayer Team
CPU vendor name: GenuineIntel  max cpuid level: 13
CPU: Intel(R) Core(TM)2 Quad CPU    Q9400  @ 2.66GHz (Family: 6, Model: 23, Stepping: 10)
extended cpuid-level: 8
extended cache-info: 201351232
Detected cache-line size is 64 bytes
CPUflags:  MMX: 1 MMX2: 1 3DNow: 0 3DNowExt: 0 SSE: 1 SSE2: 1 SSSE3: 1
Compiled for x86 CPU with extensions: MMX MMX2 SSE SSE2 SSSE3 CMOV
get_path('codecs.conf') -> '/home/lyall/.mplayer/codecs.conf'
Reading /home/lyall/.mplayer/codecs.conf: Can't open '/home/lyall/.mplayer/codecs.conf': No such file or directory
Reading /etc/mplayer/codecs.conf: Can't open '/etc/mplayer/codecs.conf': No such file or directory
Using built-in default codecs.conf.
init_freetype
Using MMX (with tiny bit MMX2) Optimized OnScreenDisplay
get_path('fonts') -> '/home/lyall/.mplayer/fonts'
Configuration: --cc=x86_64-pc-linux-gnu-gcc --host-cc=x86_64-pc-linux-gnu-gcc --prefix=/usr --bindir=/usr/bin --libdir=/usr/lib64 --confdir=/etc/mplayer --datadir=/usr/share/mplayer --mandir=/usr/share/man --disable-svga --disable-svgalib_helper --disable-ass-internal --disable-arts --disable-kai --enable-networking --enable-joystick --disable-nemesi --disable-bl --disable-bluray --disable-ftp --disable-fribidi --disable-libnut --disable-lirc --disable-lircc --disable-apple-ir --disable-cdparanoia --disable-dvdread-internal --disable-libdvdcss-internal --charset=UTF-8 --disable-tv-bsdbt848 --disable-ivtv --disable-tv-v4l1 --disable-dvb --disable-pvr --enable-radio --enable-radio-capture --disable-musepack --disable-libmpeg2-internal --disable-libbs2b --disable-libgsm --disable-liblzo --disable-librtmp --disable-libmpeg2 --disable-live --disable-mpg123 --disable-pnm --disable-libvpx-lavc --codecsdir=/usr/lib64/codecs --disable-win32dll --disable-md5sum --disable-fbdev --disable-zr --disable-mga --disable-xmga --disable-3dfx --disable-tdfxvid --disable-tdfxfb --disable-xvr100 --disable-esd --disable-jack --disable-ladspa --disable-nas --disable-openal --disable-pulse --disable-gui --disable-dxr3 --disable-ggi --enable-menu --disable-vdpau --disable-vidix --disable-vidix-pcidb --enable-xvmc --with-xvmclib=XvMCW
CommandLine: '-msglevel' 'all=6' 'Test.mp4'
Using nanosleep() timing
get_path('input.conf') -> '/home/lyall/.mplayer/input.conf'
Can't open input config file /home/lyall/.mplayer/input.conf: No such file or directory
Parsing input config file /etc/mplayer/input.conf
Input config file /etc/mplayer/input.conf parsed: 92 binds
Opening joystick device /dev/input/js0
Can't open joystick device /dev/input/js0: No such file or directory
Can't init input joystick
get_path('Test.mp4.conf') -> '/home/lyall/.mplayer/Test.mp4.conf'

Playing Test.mp4.
get_path('sub/') -> '/home/lyall/.mplayer/sub/'
[file] File size is 115662172 bytes
STREAM: [file] Test.mp4
STREAM: Description: File
STREAM: Author: Albeu
STREAM: Comment: based on the code from ??? (probably Arpi)
LAVF_check: QuickTime/MPEG-4/Motion JPEG 2000 format
libavformat file format detected.
==> Found video stream: 0
======= VIDEO Format ======
  biSize 78
  biWidth 624
  biHeight 352
  biPlanes 0
  biBitCount 24
  biCompression 875967048='H264'
  biSizeImage 658944
Unknown extra header dump: [1] [4d] [40] [1e] [ff] [e1] [0] [17] [67] [4d] [40] [1e] [d9] [0] [9c] [2d] [b0] [11] [0] [0] [3] [3] [e9] [0] [0] [bb] [80] [8f] [16] [2e] [48] [1] [0] [4] [68] [cb] [cf] [20]
===========================
[lavf] stream 0: video (h264), -vid 0
==> Found audio stream: 1
======= WAVE Format =======
Format Tag: 20557 (0x504D)
Channels: 2
Samplerate: 48000
avg byte/sec: 14011
Block align: 1
bits/sample: 16
cbSize: 2
Unknown extra header dump: [11] [90]
==========================================================================
[lavf] stream 1: audio (aac), -aid 0, -alang eng
LAVF: 1 audio and 1 video streams found
LAVF: build 3434240
VIDEO:  [H264]  624x352  24bpp  23.976 fps  602.4 kbps (73.5 kbyte/s)
[V] filefmt:44  fourcc:0x34363248  size:624x352  fps:23.976  ftime:=0.0417
Clip info:
 major_brand: M4V
 minor_version: 0
 compatible_brands: isomavc1M4A M4V mp42
 creation_time: 2012-04-29 17:14:20
 title: Test.mp4
Load subtitles in ./
get_path('sub/') -> '/home/lyall/.mplayer/sub/'
X11 opening display: :0.0
vo: X11 color mask:  FFFFFF  (R:FF0000 G:FF00 B:FF)
vo: X11 running at 2560x1024 with depth 24 and 32 bpp (":0.0" => local display)
[x11] Detected wm supports NetWM.
[x11] Detected wm supports FULLSCREEN state.
[x11] Detected wm supports BELOW state.
[x11] Detected wm supports ABOVE state.
[x11] Current fstype setting honours FULLSCREEN ABOVE BELOW X atoms
[VO_XV] Using Xv Adapter #0 (NV17 Video Texture)
[xv common] Drawing no colorkey.
[xv common] Maximum source image dimensions: 8192x8192
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
INFO: libavcodec init OK!
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
dec_audio: Allocating 192000 + 65536 = 257536 bytes for output buffer.
FFmpeg's libavcodec audio codec
INFO: libavcodec "aac" init OK!
AUDIO: 48000 Hz, 2 ch, s16le, 112.1 kbit/7.30% (ratio: 14011->192000)
Selected audio codec: [ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))
==========================================================================
Building audio filter chain for 48000Hz/2ch/s16le -> 0Hz/0ch/??...
[libaf] Adding filter dummy
[dummy] Was reinitialized: 48000Hz/2ch/s16le
[dummy] Was reinitialized: 48000Hz/2ch/s16le
Trying every known audio driver...
ao2: 48000 Hz  2 chans  s16le
audio_setup: using '/dev/dsp' dsp device
audio_setup: using '/dev/mixer' mixer device
audio_setup: using 'pcm' mixer device
audio_setup: sample format: s16le (requested: s16le)
audio_setup: using 2 channels (requested: 2)
audio_setup: using 48000 Hz samplerate (requested: 48000)
audio_setup: frags:   8/8  (8192 bytes/frag)  free:  65536
AO: [oss] 48000Hz 2ch s16le (2 bytes per sample)
AO: Description: OSS/ioctl audio output
AO: Author: A'rpi
Building audio filter chain for 48000Hz/2ch/s16le -> 48000Hz/2ch/s16le...
[dummy] Was reinitialized: 48000Hz/2ch/s16le
[dummy] Was reinitialized: 48000Hz/2ch/s16le
Starting playback...
Increasing filtered audio buffer size from 0 to 65536
[ffmpeg] aspect_ratio: 1.772727
VDec: vo config request - 624 x 352 (preferred colorspace: Planar YV12)
Trying filter chain: vo
VDec: using Planar YV12 as output csp (no 0)
Movie-Aspect is 1.77:1 - prescaling to correct movie aspect.
VO Config (624x352->624x352,flags=0,'MPlayer',0x32315659)
VO: [xv] 624x352 => 624x352 Planar YV12
VO: Description: X11/Xv
VO: Author: Gerd Knorr <kraxel@goldbach.in-berlin.de> and others
Xvideo image format: 0x32595559 (YUY2) packed
Xvideo image format: 0x32315659 (YV12) planar
Xvideo image format: 0x59565955 (UYVY) packed
Xvideo image format: 0x30323449 (I420) planar
using Xvideo port 550 for hw scaling
*** [vo] Exporting mp_image_t, 624x352x12bpp YUV planar, 329472 bytes
Unicode font: 5179 glyphs.
Unicode font: 5179 glyphs.
A:   3.4 V:   3.4 A-V:  0.000 ct:  0.000   0/  0  2%  0%  0.3% 0 0                                                                 
Uninit audio filters...
[libaf] Removing filter dummy
Uninit audio: ffmpeg
Uninit video: ffmpeg
vo: uninit ...


XavierMiller wrote:
Hello,

Can you check the input and output rates, the difference could come from
- different audio rates (44.1 kHz vs 48 kHz)
- video frame rate (24, 25, 29.5 or 30 fps)

What is your display frame rate ?

_________________
...Lyall
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1552
Location: Adelaide/Australia

PostPosted: Tue Aug 28, 2012 2:15 am    Post subject: Reply with quote

No suggestions?
I would hope that I can script up something using ffmpeg or mplayer/mencoder where I can simply set it going and have the videos fixed next day.
I would rather not have to use a GUI.
_________________
...Lyall
Back to top
View user's profile Send private message
Ion Silverbolt
Apprentice
Apprentice


Joined: 04 Nov 2004
Posts: 203

PostPosted: Tue Aug 28, 2012 4:22 am    Post subject: Reply with quote

Are you sure it's your recording that's the problem? I get that same effect whenever I have a slow processor, or GPU lag. Recently I had the problem because of something conky was doing.

Do you get the same delay if you play the mp4 files in vlc?
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1552
Location: Adelaide/Australia

PostPosted: Tue Aug 28, 2012 4:27 am    Post subject: Reply with quote

The slowdown happens in
  • mplayer
  • vlc
  • via mediatomb on my PS3


It starts in sync, then gradually, over the course of the 30 minute show, it gets more and more out of sync until, by 30 minutes, it's about 1 second out.

From reading, it is a problem with the encoding, whereby the sound card is not recording at exactly the frequency it was supposed to.

My machine is an intel Core 2 Q9400 @ 2.66Ghz. Plenty fast enough to play small (Standard Def) videos.
_________________
...Lyall
Back to top
View user's profile Send private message
cord
Guru
Guru


Joined: 28 Apr 2007
Posts: 344

PostPosted: Tue Aug 28, 2012 12:21 pm    Post subject: Reply with quote

lyallp, is your video correct (in sync) by itself?
show us mediainfo output
Back to top
View user's profile Send private message
aCOSwt
Bodhisattva
Bodhisattva


Joined: 19 Oct 2007
Posts: 2537
Location: Hilbert space

PostPosted: Tue Aug 28, 2012 1:12 pm    Post subject: Reply with quote

lyallp wrote:
No suggestions?

aCOSwt wrote:
would the original video track have been in 23.976 fps then converted in 25 forgetting to adjust the sound track ?

I fear you did not answer my stupid question. :twisted:
_________________
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1552
Location: Adelaide/Australia

PostPosted: Tue Aug 28, 2012 9:45 pm    Post subject: Reply with quote

Quote:
I fear you did not answer my stupid question. :twisted:

I thought I had, by giving the mplayer output, which shows the values of frame rates, recording frequencies, etc. With regard to conversions, I have no idea :)
_________________
...Lyall
Back to top
View user's profile Send private message
pictionary
n00b
n00b


Joined: 31 Jul 2013
Posts: 2
Location: Adelaide

PostPosted: Wed Jul 31, 2013 9:49 am    Post subject: Solved? Reply with quote

I too experienced exactly this with a mp4 movie. ( fine at start, about a second audio delay by end).
It was fine playing on VLC on my PC, but my cheap PVR always has crappy increasing-delay.
I just solved it to my satisfaction, and thought Id share.
Heres how I did it.

1. Estimate delta in time at end of movie ( 1 second in this case).
2. Extract Audio to mp3. ( in windows, it was "
Code:
ffmpeg -i <filenmae> -acodec libmp3lame <output filanme>
" )
3. Use audacity's "Effect -> Change tempo" , to change length of audio track. ..Select entire track(s), then Change-tempo slider bar to select new end time ( = old end time)
3.a Extract as mp3
4. Use ffmpeg to remap ( replace) the new audio track over the old one..
"
Code:
ffmpeg -i <input movie filename> -i <newmp3file> -map 0:0 -map 1 -vcodec copy -acodec copy <output movie filename>
"
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1552
Location: Adelaide/Australia

PostPosted: Wed Jul 31, 2013 11:03 am    Post subject: Reply with quote

Thanks for that.

I was hoping for something I could script, to fix a batch of files, but this does give me at least one way of fixing things.

:)
_________________
...Lyall
Back to top
View user's profile Send private message
Tuna
Guru
Guru


Joined: 19 Jul 2002
Posts: 485
Location: Berlin

PostPosted: Wed Jul 31, 2013 12:19 pm    Post subject: Reply with quote

You can script it - sort of - with some assumptions.

Look at MP4Box/GPAC.

Code:

# get mp4 info
$ MP4Box -info input.mp4

[..] assume that audio and video track duration should match
[..] calculate a new fps value so the video duration matches with the audio (number of samples and some math..)

[..] check/grep which track id is your video

# extract raw video track
$ MP4Box input.mp4 -raw <trackid>

# create new mp4 from raw video
$ MP4Box -add input_track<trackid>.h264 -fps <fps> out.mp4

# copy audio from original mp4 to new mp4
$ MP4Box -add input.mp4#audio out.mp4


Perhaps the procedure can be simplified.. but that was out of head.. its been some time.. hopefully there are not too many erros in the example..
Back to top
View user's profile Send private message
pictionary
n00b
n00b


Joined: 31 Jul 2013
Posts: 2
Location: Adelaide

PostPosted: Sun Aug 04, 2013 7:24 am    Post subject: Reply with quote

I didnt put too much thought into my solution. but IMHO, I think shifting the tempo of the audio is smarter than decoding / re-encoding the video ( which is CPU intensive, and can reduce quality if not done optimally). I'd be surprised if there wasnt an cmd-line program floating around on internet than could alter audio tempo appropriately... maybe audacity has cmd-line options?
If not, Id be happy to write some simple code for you that does it (eg in cmd-line language 'octave') in exchange for a slab of good beer. :wink:
Back to top
View user's profile Send private message
Tuna
Guru
Guru


Joined: 19 Jul 2002
Posts: 485
Location: Berlin

PostPosted: Mon Aug 05, 2013 9:04 am    Post subject: Reply with quote

The MP4Box approach does not alter, change or re-encode audio or video. It simply muxes the two tracks with slightly altered time stamps for the video track. So the process can be "reversed" without any loss of the original signal. The process should require almost no CPU time and is solely limited to I/O throughput.

The proposed solution assumes that audio and video tracks start and end at the same time. If there is an offset between the two or if they have different running times this will obviously fail. In that case there is no way to automate this process as it requires human perception to sync the audio and video tracks.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Multimedia All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum