[Libav-user] Troubles with transcoding

hatred adrozdoff at gmail.com
Thu Nov 22 04:32:06 CET 2012

Hi, Hannes!

My comments below. Worked code also.

2012/11/21 Hannes Wuerfel <hannes.wuerfel at student.hpi.uni-potsdam.de>

> Am 21.11.2012 07:42, schrieb hatred:
> In my case, I'd like to manipulate video frames of the file with some
> image processing kernels etc. and write the file with the same codecs they
> have back to disc, but of cause with processed frames.
> So basically I need a "transcoder" from the codec to the same codec.
> What I didn't have is your code fragment:
> "if (outCtx.videoCodecCtx->coded_frame->pts != AV_NOPTS_VALUE)
> {
>                         outPkt.pts =
> av_rescale_q(outCtx.videoCodecCtx->coded_frame->pts,
> outCtx.videoCodecCtx->time_base,
> outCtx.videoStream->time_base);
> }"

In common way Stream time base and CodecContext time base is not equal, but
regard comments in AVPacket structure pts field must be in Stream time base
units and I rescale it before muxing

> If I put it into my code almost all or no video frames are encoded
> succesfully and the output is:
> [libx264 @ 0000000002620080] specified frame type (3) at 1314 is not
> compatible with keyframe interval
> [mp4 @ 00000000034ff080] pts (0) < dts (1352000) in stream 0
> Error while writing video frame
It is my error: we should rescale dts field also if it is not equal to

> I looked into ffmpeg.c as well but I had no time to step threw it while
> transcoding, because I'm currently working on a windows machine.
>  Perhaps you can explane to me why we have to rescale the presentation
> time stamp...

See comments above. For encoding we can set same time base to stream and
codec context and remove pts/dts rescaling.

> Ah and you've rescaled the audio too. I did not rescale the audio but for
> every input codec I take, audio is always fine, even withouth:
> "if (frame->pts == AV_NOPTS_VALUE)
> {
>                     AVRational samplesRateInv = {1,
> outCtx.audioCodecCtx->sample_rate};
> int64_t pts = inputAudioSamples;
>                     frame->pts = av_rescale_q(pts, samplesRateInv,
> inCtx.audioCodecCtx->time_base);
> }
>                 inputAudioSamples += frame->nb_samples;"
>  Why is this?

This part of code is root couse of trouble with sound encoding and muxing.
After deconding audio samples pts value always equal to AV_NOPTS_VALUE, so
I tried to calculate real value using sample_rate value and decoded samples
count. But I suppose that muxer don't want valid pts values for audio
packets it want NOPTS value for correct muxing.

Worked code: http://pastebin.com/zYTiuRyA

But I also have some amount of questions:
1. How to correctly resample audio frames if input and output streams have
different Sample Format, Sample Rate, Channels Count, Channels Layout or
Frame Size? Example wanted...
2. How to correctly synchronize audio and video? In some cases in
transcoded file video and sound not synchronized and I don't understand -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121122/584826bc/attachment.html>

More information about the Libav-user mailing list