Transcoding with ffmpeg libfaac reduces the audio duration - ffmpeg

I have to transcode a mpegts file: input.ts(H.264 and AAC)
So I use the following command line:
ffmpeg -y -i input.ts -acodec libfaac -vcodec copy out.ts
But I find that the duration of output.ts's audio is shorter than that of the input.ts!
If I do the following iterations, the output's audio will be shorter and shorter.
ffmpeg -y -i out.ts -acodec libfaac -vcodec copy out-iter1.ts
ffmpeg -y -i out-iter1.ts -acodec libfaac -vcodec copy out-iter2.ts
ffmpeg -y -i out-iter2.ts -acodec libfaac -vcodec copy out-iter3.ts
ffmpeg -y -i out-iter3.ts -acodec libfaac -vcodec copy out-iter4.ts
ffmpeg -y -i out-iter4.ts -acodec libfaac -vcodec copy out-iter5.ts
My ffmpeg's version is 0.6.6. libfaac's version is 1.28.
input.ts's audio duration is 10.432 seconds. out-iter5.ts's audio duration is 10.56 seconds
And I have also tried ffmpeg with version 0.11.
And it makes the audio longer than the original input.
So my question is: how to make sure the output's audio duration is same as the input's audio.
Since libfaac will make the audio shorter, how can I compensate for it?
(For some reason I can't use -acodec copy directly.) Any suggestions?

Related

Problems with modifying encoder metadata with FFMPEG

I'm trying to change FFMPEG encoder writing application with FFMPEG -metadata and for whatever reason, it's reading the input but not actually writing anything out.
-map_metadata -metadata:s:v:0 -metadata writing_application, basically every single stack overflow and stack exchange thread, but they all won't write to the file at all.
ffmpeg -i x.mp4 -s 1920x1080 -r 59.94 -c:v h264_nvenc -b:v 6000k -vf yadif=1 -preset fast -fflags +bitexact -flags:v +bitexact -flags:a +bitexact -ac 2 x.mp4
ffmpeg -i x.mp4 -c:v copy -c:a copy -metadata Encoder="TeXT Encoder" -fflags +bitexact -flags:v +bitexact -flags:a +bitexact test.mp4
ffmpeg -i x.mp4 -vcodec copy -acodec copy -map_metadata out.mp4
ffmpeg -i x.mp4 -vcodec copy -acodec copy -metadata encoder="Encoder" -metadata comment="XX" testmeta.mp4
ffmpeg -i x.ts -c:v copy -c:a copy -metadata:s:v:0 h264 ISFT='TeXT' x.mp4
ffmpeg -i x.mp4 -i FFMETADATAFILE -map_metadata 1 -codec copy testcopy.mp4
ffmpeg -i x.ts -f ffmetadata FF
METADATAFILE
I tried to extracting the data and rewrite it back with FFMETADATAFILE but it doesn't show up. Tried forcing ffmpeg to write without any emtadata and write it back but doesn't work. Was wondering if I can write my own encoder that writes the specific encoder name, like how Handbrake/Lavf writes the encoder application into the METADATA of the video file. Or just use FFMPEG and modify the METADATA natively.
To set the writing application (mediainfo) or encoder (ffmpeg) for MP4s, use
ffmpeg -i input {-encoding parameters} -metadata:g encoding_tool=myapp out.mp4

FFmpeg adding click to beginning of audio

I've got an odd issue that's been bugging me for a while. I'm converting another format to video using FFmpeg; the conversion takes place prior and is fed into FFmpeg to be finally converted to an mp4.
Oddly, I seem to be getting a little click at the start of the resulting video; it's not present in the original audio but shows up in the final video.
Here is the sample audio. You'll notice that it has no pop at the start.
Here is the raw video input.
Here is the video my command is generating.
Here is the command I'm using to reproduce the issue (the actual conversion takes place in a Python script feeding FFmpeg the video via stdin and the audio via a temp file)
cat debug_raw_video.bin| ffmpeg -hide_banner -loglevel info -y -s 256x192 -r 30 -f rawvideo -thread_queue_size 600 -pix_fmt rgb8 -i pipe:0 -f s16le -ar 11025 -ac 1 -guess_layout_max 0 -i ./debug_audio.wav -vcodec libx264 -pix_fmt yuv420p -movflags faststart -acodec aac -strict experimental -vf scale=512:384:flags=neighbor -threads 0 -preset medium -tune animation ./out.mp4
FFmpeg version:
ffmpeg version 2.8.15 Copyright (c) 2000-2018 the FFmpeg developers
Also have the same issue with this version:
ffmpeg version 3.3.4-static http://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2017 the FFmpeg developers
Why am I getting a little click/pop at the beginning? I've been trying to figure this out for quite a while.
It appears you're specifying that the input audio is raw, but it's not:
$ file debug_audio.wav
debug_audio.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 11025 Hz
So I imagine the click you're hearing is the wav header being processed as audio. If I remove the related options, -f s16le and -ar 11025, ffmpeg correctly determines that the audio input is in wav format and produces a click-less output:
cat debug_raw_video.bin | ffmpeg -hide_banner -loglevel info -y -s 256x192 -r 30 -f rawvideo -thread_queue_size 600 -pix_fmt rgb8 -i pipe:0 -ac 1 -i ./debug_audio.wav -vcodec libx264 -pix_fmt yuv420p -movflags faststart -acodec aac -strict experimental -vf scale=512:384:flags=neighbor -threads 0 -preset medium -tune animation ./out.mp4

FFMPEG "-to" option won't stop encoding at the implied time

I'm using ffmpeg to cut and covert a part of a long video but when using the "-to" option ffmpeg keeps encoding till the end of the video and wont stop.
Here's my command:
ffmpeg -ss 00:22:59 -i input.mkv -to 00:23:15.5 -c:v libx264 -c:a aac output.mp4
if i change to "-t" and enter my desired duration, problem goes away. but since i have a very long video and many tasks i need to use "-to" which is easier for me.
Thanks in advance.
Basically, -ss when used as an input option, seeks to the given time and resets timestamps, so that the first selected frame has timestamp 0. So, in your command, t and to will have the same effect.
You have to either use
ffmpeg -ss 00:22:59 -copyts -i input.mkv -to 00:23:15.5 -c:v libx264 -c:a aac output.mp4
(not recommended)
or
ffmpeg -ss 00:22:59 -to 00:23:15.5 -i input.mkv -c:v libx264 -c:a aac output.mp4
(needs ffmpeg build > Nov 19 2017)
or
ffmpeg -i input.mkv -ss 00:22:59 -to 00:23:15.5 -c:v libx264 -c:a aac output.mp4
(slowest of the three)

ffmpeg error when I make TS file

I use ffmpeg to make video from audio/image to mp4.
I want merge/concatenate two mp4 video.
I have this to make mp4 video:
ffmpeg -i "background.png" -i "audio.mp3" -y -r 30 -b 2500k -acodec ac3 -ab 384k -vcodec mpeg4 "result.mp4"
This, it works!
To concatenate two mp4 video, I must pass by ts files.
ffmpeg -i "result.mp4" -codec copy -bsf:v hevc_mp4toannexb intermediate1.ts
But, I have this error:

FFmpeg concat and then add soundtrack results in a soundtrack that stutters

I am trying to create a video composed of clips of images and videos. For the clips of images, I use ffmpeg to create a video file and then I add a silent audio stream through these two steps:
ffmpeg.exe -loop 1 -i MyImage.png -codec:v libx264 -t 4.0 -profile:v high -preset slow -r 25 -b:v 500k -maxrate 500k -pix_fmt yuv420p -vf scale=1280:720 MyImageMovie.mp4
ffmpeg.exe -f lavfi -i anullsrc=r=48000 -i MyImageMovie.mp4 -shortest -c:v copy -c:a aac -strict experimental -y MyImageMovieWithSilentAudioStream.mp4
Then I combine my video clips and image clips with
ffmpeg.exe -f concat -i videoList.txt -c copy -y concatVideo.mp4
At this point, the video looks good, any video clips that have audio streams seemed well synced to the video.
Now I add a soundtrack:
ffmpeg.exe -i concatVideo.mp4 -i soundtrack.mp3 -ar 48000 -filter_complex "[1:a]apad [b] ; [0:a][b]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -ac 2 -shortest -y FinalVideo.mp4
The problem is that the soundtrack on FinalVideo.mp4 stutters at some (not all) of the concatenation joints.
I suspect it has to do with the audio stream and the video stream of the Image clips not being perfectly aligned. The aac has .0231s resolution and the video has 0.04s resolution. When I ffprobe the MyImageMovieWithSilentAudioStream.mp4 the duration is 4.00s but the start is 0.0213.
If my concatenated video has several of these image clips, the error can start to accumulate.
What can I do to keep the video and audio in sync and add a soundtrack that doesn't stutter?
Also, this is a little interesting, I don't hear the stutter when the final video is played on Windows Media Player, but it is there if I play it on VLC or via the html native video element.
Try adding the soundtrack in the same step as the concat.
ffmpeg -f concat -i videoList.txt -i soundtrack.mp3 \
-filter_complex "[1:a]apad[b];[0:a][b]amerge=inputs=2[a]" \
-map 0:v -map "[a]"
-c:v copy -c:a aac -ac 2 -ar 48000 -shortest -y FinalVideo.mp4
As an aside, you can also combine the image and silent stream generation,
ffmpeg -loop 1 -i MyImage.png -f lavfi -i anullsrc=r=48000 \
-vf scale=1280:720 \
-c:v libx264 -profile:v high -preset slow -r 25 -b:v 500k -maxrate 500k -pix_fmt yuv420p \
-c:a aac -strict experimental -t 4 -y MyImageMovieWithSilentAudioStream.mp4

Resources