DVCPRO adds 1 frame to the file metadata

Here you can submit bugreports
movalex
Posts: 36
Joined: Thu Feb 06, 2020 2:23 pm

DVCPRO adds 1 frame to the file metadata

Post by movalex »

I convert SD file to the DVCPRO format with addintional processing. The Source file is 1m0s long, with interlacing and it has audio interleave duration of 1000 ms (25 frames).
When I convert it, the interleave duration becomes 40ms (1 frame), and somehow the host app that airs this file, reports the file is 1 frame longer than 1 minute.
The only way I can solve this is preprocess the file in Resolve, making the duration 1 frame shorter. Then after ffastrans processing, it becomes 1m0s video and 59.965ms audio, which is accepted as correct.
Can I solve this by adjusting the interleave duration of the DVCPRO encoder? Is it possible with FFAStrans?

Here's the ffmpeg command executed by ffastrans:

Code: Select all

-f lavfi -i "aevalsrc=0" -f lavfi -i "color=color=black:size=720x576" -shortest -map_metadata -1 -map 0:0 -filter_complex "[0:1]pan=1|c0=c0,aresample=osr=192000:osf=s32,alimiter=1:1:0.875:0.1:level=0,aresample=ochl=1[a1],[0:1]pan=1|c0=c1,aresample=osr=192000:osf=s32,alimiter=1:1:0.875:0.1:level=0,aresample=ochl=1[a2],[a1][a2]amerge=2,apad[astr1]" -map "[astr1]" -c:a:0 pcm_s16le -ar:a:0 48000 -vf "sidedata=delete,metadata=delete,setrange=tv,crop=w=in_w:h=540:x=0:y=18,colorspace=fast=1:ispace=smpte170m:itrc=smpte170m:iprimaries=bt470bg:all=bt601-6-625,bwdif=mode=1:parity=1,scale=w=720:h=576:flags=lanczos,framerate=50,tinterlace=mode=5,scale=interl=1,format=yuv420p,setfield=bff,setsar=r=1.06666666666667:max=16" -timecode 00:00:00:00 -vtag dvsd -c:v dvvideo -r 25 -top 0 -field_order bb -pix_fmt yuv420p -b:v 25000000 -minrate 25000000 -maxrate 25000000 -color_primaries bt470bg -color_trc smpte170m -colorspace bt470bg -color_range tv -chroma_sample_location topleft -signal_standard 1 -f avi -max_muxing_queue_size 700 -map_metadata -1 -metadata "creation_time=now" -y 
and this is mediainfo of the source:
Format : AVI
Format/Info : Audio Video Interleave
Commercial name : DV
File size : 221 MiB
Duration : 1 min 0 s
Overall bit rate mode : Constant
Overall bit rate : 30.9 Mb/s
Recorded date : 2024-08-12T14:13:26+03:00
Writing application : Adobe Photoshop 23.2 (20220118.orig.521 930aa48) (Windows)

Video
ID : 0
Format : DV
Codec ID : dvsd
Codec ID/Hint : Sony
Duration : 1 min 0 s
Bit rate mode : Constant
Bit rate : 24.4 Mb/s
Width : 720 pixels
Height : 576 pixels
Display aspect ratio : 4:3
Frame rate mode : Constant
Frame rate : 25.000 FPS
Standard : PAL
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Interlaced
Scan order : Bottom Field First
Compression mode : Lossy
Bits/(Pixel*Frame) : 2.357
Time code of first frame : 00:00:00:00 / 00:00:00:00
Time code source : Adobe tc_A / Adobe tc_O
Stream size : 206 MiB (93%)

Audio
ID : 1
Format : PCM
Format settings : Little / Signed
Codec ID : 1
Duration : 1 min 0 s
Bit rate mode : Constant
Bit rate : 1 536 kb/s
Channel(s) : 2 channels
Sampling rate : 48.0 kHz
Bit depth : 16 bits
Stream size : 11.0 MiB (5%)
Alignment : Aligned on interleaves
Interleave, duration : 1000 ms (25.00 video frames)
Interleave, preload duration : 1000 ms
and the info of the wrong length:
Format : AVI
Format/Info : Audio Video Interleave
Commercial name : DV
File size : 217 MiB
Duration : 1 min 0 s
Overall bit rate mode : Constant
Overall bit rate : 30.3 Mb/s
Writing application : Lavf60.17.100

Video
ID : 0
Format : DV
Codec ID : dvsd
Codec ID/Hint : Sony
Duration : 1 min 0 s
Bit rate mode : Constant
Bit rate : 24.4 Mb/s
Width : 720 pixels
Height : 576 pixels
Display aspect ratio : 4:3
Frame rate mode : Constant
Frame rate : 25.000 FPS
Standard : PAL
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Interlaced
Scan order : Bottom Field First
Compression mode : Lossy
Bits/(Pixel*Frame) : 2.357
Time code of first frame : 00:00:00:00
Time code source : ISMP
Stream size : 206 MiB (95%)

Audio
ID : 1
Format : PCM
Format settings : Little / Signed
Codec ID : 1
Duration : 1 min 0 s
Bit rate mode : Constant
Bit rate : 1 536 kb/s
Channel(s) : 2 channels
Sampling rate : 48.0 kHz
Bit depth : 16 bits
Stream size : 11.0 MiB (5%)
Alignment : Aligned on interleaves
Interleave, duration : 40 ms (1.00 video frame)
And this is audio info of the "correct" file:
Audio
ID : 1
Format : PCM
Format settings : Little / Signed
Codec ID : 1
Duration : 59 s 965 ms
Bit rate mode : Constant
Bit rate : 1 536 kb/s
Channel(s) : 2 channels
Sampling rate : 48.0 kHz
Bit depth : 16 bits
Stream size : 11.0 MiB (5%)
Alignment : Aligned on interleaves
Interleave, duration : 40 ms (1.00 video frame)
User avatar
FranceBB
Posts: 258
Joined: Sat Jun 25, 2016 3:43 pm
Contact:

Re: DVCPRO adds 1 frame to the file metadata

Post by FranceBB »

Hi movalex,
I think this is a side effect of one very important check that the filter_builder makes. Effectively, the filter_builder checks that the audio and video are exactly of the same length and if they're not it pads them. In this case, the padding makes it result as 1 frame longer. I know that in Mediainfo you're seeing both being at 1 minute but it might be worth using the detailed view to see exactly how long they last. Even then, it might be just the container lying. What I generally do in this case is to index the file in Avisynth with LWLibavVideoSource() and LWLibavAudioSource() and then check them in avspmod mod. Anyway, if your source is consistent and you always get 1 added frame consistently and you see it "fixed" when you get rid of one frame, then you can technically do one thing: trim it.

Effectively what I'm thinking about is:

Watch -> A/V Decoder (Indexing) -> Custom AVS Script (trim) -> DVCPRO Encoder.

Make sure to set the A/V Decoder to "no decode" and "same as source" for both audio and video. Oh and untick the force 8bit thingie.

In the Custom AVS Script you can do something like:

Code: Select all

my_last_frame=m_clip.FrameCount()-1
m_clip=Trim(m_clip, 0, my_last_frame)

return m_clip
This will remove one frame, thus hopefully reproducing what you were doing in Adobe Premiere.
Please make sure to untick the "clear media properties" tickbox in the Custom AVS Script.


Let me know how it goes and keep in mind that unfortunately I don't have my PC in front of me this very moment so I can't test the script but I will on Monday.
movalex
Posts: 36
Joined: Thu Feb 06, 2020 2:23 pm

Re: DVCPRO adds 1 frame to the file metadata

Post by movalex »

Thanks, will check that!
Also how exactly do you check the length with avspmod? My intention is to check if the length is not longer or shorter than a whole amount of minutes, like 1, 2 or 3.
User avatar
FranceBB
Posts: 258
Joined: Sat Jun 25, 2016 3:43 pm
Contact:

Re: DVCPRO adds 1 frame to the file metadata

Post by FranceBB »

movalex wrote: Sun Aug 18, 2024 12:37 am Thanks, will check that!
Let me know how it goes. :)
In the meantime I did check it with an actual file and it worked. :D
movalex wrote: Sun Aug 18, 2024 12:37 am Also how exactly do you check the length with avspmod?
Oh, that's pretty easy, the first thing I've done was indexing a file with LWLibav and after that I checked at the bottom left how many frames it counted:

Code: Select all

video=LWLibavvideoSource("\\mibctvan000\Ingest\MEDIA\temp\Emotion_UK\M9101400_03.mxf")
audio=LWLibavAudioSource("\\mibctvan000\Ingest\MEDIA\temp\Emotion_UK\M9101400_03.mxf", stream_index=1)
AudioDub(video, audio)
Screenshot from 2024-08-20 18-32-30.png
Screenshot from 2024-08-20 18-32-30.png (1.39 KiB) Viewed 5450 times
Then I added the code I posted before, but without the m_clip 'cause that's something you only need in FFAStrans:

Code: Select all

video=LWLibavvideoSource("\\mibctvan000\Ingest\MEDIA\temp\Emotion_UK\M9101400_03.mxf")
audio=LWLibavAudioSource("\\mibctvan000\Ingest\MEDIA\temp\Emotion_UK\M9101400_03.mxf", stream_index=1)
AudioDub(video, audio)


my_last_frame=FrameCount()-1
Trim(0, my_last_frame)
and sure enough it was one frame less:
Screenshot from 2024-08-20 18-35-31.png
Screenshot from 2024-08-20 18-35-31.png (1.27 KiB) Viewed 5450 times

And you can of course increase the value to get rid of more frames like -2 -3 -4 -5 etc.
In FFAStrans it would be:

Code: Select all

my_last_frame=m_clip.FrameCount()-1
m_clip=Trim(m_clip, 0, my_last_frame)

return m_clip
but you can also try with:

Code: Select all

my_last_frame=m_clip.FrameCount()-2
m_clip=Trim(m_clip, 0, my_last_frame)

return m_clip
and so on by increasing the frame count until you get the sweet spot.
Now, if you had to measure and make sure that the clips are all of a very specific length, like, let's say, all 1 minute long or all 2 minutes long etc, then it's gonna be slightly more complicated as you need to know the input framerate and then add a bunch of conditionals to check the length and convert the timecode into frames so that you're gonna know how many frames need to be trimmed.
Anyway, for now let's just stick to the simple solution of trimming one frame out from the end and let's see where we go from there. :)
movalex
Posts: 36
Joined: Thu Feb 06, 2020 2:23 pm

Re: DVCPRO adds 1 frame to the file metadata

Post by movalex »

Thank you @FranceBB, the Avisynth frame clipping command works as you described.
However, just to be sure, I downgraded my FFASTrans deploy from 1.4.0 to the previous version 1.3.1, and with the same workflow and the same file I got the correct output.
I believe this is something that was introduced in the 1.4.0 that adds additional data to the DVCPRO encoder.
Here's some details:

1min file (SD 720x576) converted to DVCPRO with 1.4.0 produces 1m 0s file with the file length of 222410 Kb (incorrect file length, 1 frame extra)

Code: Select all

VIDEO:
Duration                                 : 60040
Duration                                 : 1 min 0 s
Duration                                 : 1 min 0 s 40 ms
Duration                                 : 1 min 0 s
Duration                                 : 00:01:00.040
Duration                                 : 00:01:00:01
Duration                                 : 00:01:00.040 (00:01:00:01)

AUDIO:
Duration                                 : 60005
Duration                                 : 1 min 0 s
Duration                                 : 1 min 0 s 5 ms
Duration                                 : 1 min 0 s
Duration                                 : 00:01:00.005
Duration                                 : 00:01:00.005

File size                                : 227603090
1min file converted to DVCPRO with 1.3.1 produces 1m 0s file with the file length of 222269 Kb (correct file length)

Code: Select all

VIDEO:
Duration                                 : 60000
Duration                                 : 1 min 0 s
Duration                                 : 1 min 0 s 0 ms
Duration                                 : 1 min 0 s
Duration                                 : 00:01:00.000
Duration                                 : 00:01:00:00
Duration                                 : 00:01:00.000 (00:01:00:00)

AUDIO:
Duration                                 : 60005
Duration                                 : 1 min 0 s
Duration                                 : 1 min 0 s 5 ms
Duration                                 : 1 min 0 s
Duration                                 : 00:01:00.005
Duration                                 : 00:01:00:00
Duration                                 : 00:01:00.005 (00:01:00:00)

File size                                : 227747114
Note that the duration of the audio in the second file is still 5ms longer than the source, but that seems acceptable.
I will include the workflow too. Hopefully @emcodem would be able to check that too.

UPD: I also tried to convert HD to SD DV with the same workflow, but again got the wrong length, this time in v1.3.1 too. This time the audio track becomes 91ms longer than the one in the the source file, and the video track is 40 ms longer.

Code: Select all

VIDEO:
Duration                                 : 60040
Duration                                 : 1 min 0 s
Duration                                 : 1 min 0 s 40 ms
Duration                                 : 1 min 0 s
Duration                                 : 00:01:00.040
Duration                                 : 00:01:00:01
Duration                                 : 00:01:00.040 (00:01:00:01)

AUDIO:
Duration                                 : 60091
Duration                                 : 1 min 0 s
Duration                                 : 1 min 0 s 91 ms
Duration                                 : 1 min 0 s
Duration                                 : 00:01:00.091
Duration                                 : 00:01:00.091

File size                                : 227619474
This all seem pretty random to me. Hopefully you have an idea what is happening here.

UPD2: Is it possible thats an ffmpeg version bug? I'm not sure how to check for that, dvcpro encoder command seems pretty complicated to reproduce. Is there a better way to encode dvvideo without any duration issues?
Attachments
NAEFIR SD DV.json
(16.99 KiB) Downloaded 68 times
emcodem
Posts: 1749
Joined: Wed Sep 19, 2018 8:11 am

Re: DVCPRO adds 1 frame to the file metadata

Post by emcodem »

Hi,

i guess it depends on ffmpeg version. As far as i can see, it is caused by the filter "apad" which is inserted automatically into the ffmpeg filter chain by ffastrans and according to my tests, the amount of unneccessary added audio depends a little on ffmpeg version/build.
Basically it is not "acceptable" to see 5 ms audio too much in the output when input and output is PCM audio, such stuff should only happen with e.g. aac that does not have a frame size that matches the video.

A number of possible workarounds comes to my head, one is to use the s_options feature but this is only supported from 1.4 onwards.

So, what we want to do? We want to "alter" the ffastrans internally calculated encoding command and just remove the "apad" filter.
How to do this using s_options:

1) before the encoder processor, insert a populate proc and set builtin variable s_options to : skip_encoding=True|save_enc_cmd=True
2) this really "disables" the encoding, so the encoding processor will just calculate the ffmpeg command and populate it to the inbuilt variable "%s_last_enc_cmd%"
3) from here all we need to do is to replace ",apad" from s_last_enc_cmd and after that just use a cmd processor to execute the calculated command line
4) no need to think about s_source or anything else, everything works just as if the encoding processor would have encoded normally


After these 3 steps, the audio had exactly the same duration as video for me but i noticed that exactly 2 frames were missing in the output. As i see this is done "intentionally" by the "Fade" processor. To disable this behaviour, we need to check "Add black frames" in the Fade processor.

Try this. Changes are exactly as mentioned above, including the Add black frames checkbox in Fade proc.
NAEFIR SD DV_emcodem.json
(19.96 KiB) Downloaded 68 times
emcodem, wrapping since 2009 you got the rhyme?
movalex
Posts: 36
Joined: Thu Feb 06, 2020 2:23 pm

Re: DVCPRO adds 1 frame to the file metadata

Post by movalex »

OMG, this works so well. Thank you a million times, @emcodem, I would never come to this by my own. Now the length is exact for both audio and video (v1.4.0.7)

1. While this workaround works, would that be better to add an option in the encoder to disable the apad, for those who may encounter that too?
2. Not sure how the Fade processor and the encoder are interacting here, and why is it needed to add the frames during the fade. It is possible that the "Fade" does not actually add those frames, but removes them by default, unless the "add" option is enabled?
emcodem
Posts: 1749
Joined: Wed Sep 19, 2018 8:11 am

Re: DVCPRO adds 1 frame to the file metadata

Post by emcodem »

Thats very good questions.
1. adding an option to disable apad would just be another workaround but not eliminate the issue. Currently apad is more or less always inserted, "just in case". Steinar obviously assumed that the apad filter works flawlessly - which it should. The solution to the problem would be to pinpoint the problem and get the apad filter working as expected.
Of course there are chances that the problem is not even about apad filter but something else, we will see

2. Absolutely, well spotted. The checkboxes name is terribly wrong chosen because when it's checked it inserts an avisynth line to cut off first and last frames. So no frames are inserted as the name implies but frames are cut instead.
emcodem, wrapping since 2009 you got the rhyme?
admin
Site Admin
Posts: 1680
Joined: Sat Feb 08, 2014 10:39 pm

Re: DVCPRO adds 1 frame to the file metadata

Post by admin »

Hey guys,

About 2.

The default AviSynth behavior of the fade filter is to add one black frame at at the start/end of the clip. And this is also how it was originally implemented in FFAStrans as well. But some would complain that the duration of the clip increased (by one or two frames) and thus the option was added in order to work around that issue. So I would argue that the name of the option is correct as it's just descriptive of the fade default behavior if you leave it checked.

-steinar
emcodem
Posts: 1749
Joined: Wed Sep 19, 2018 8:11 am

Re: DVCPRO adds 1 frame to the file metadata

Post by emcodem »

Thanks Steinar, so the naming is fine :D
Also, the information that Fadein and Out adds frames helps a lot.

I believe i understand it now completely: the bare existence of ffmpeg apad filter is of course not the problem (it can only fuck up if it actually has something to compensate), the question is why it actually comes into play, so at which point do we produce shorter audio than video.
My conclusion is that the unchecked checkbox "fade audio" and "add black frames" causes the problem. (they are by default all checked but not in @movalex workflow)

This is the avs script part produced by the fade filter when these 2 checkboxes are unchecked.

Code: Select all

m_clip = Trim(m_clip, 1, m_clip.FrameCount-2)
audio = m_clip
video = m_clip
video = Fadein(video, Round(6))
video = Fadeout(video, Round(6))
m_clip = AudioDub(video, audio)
Return m_clip
We see that we first trim 2 frames from the original file (1 at start, 1 at end), then we split audio and video and add 2 frames to the video again by applying the Fadein/out functions - ending up in 2 frames longer video than audio. At the end, we merge (audiodub) the shorter audio and longer video. (we also moved the A/V sync 1 frame)
On the way, i noticed that Fadein does in fact add 2 black frames at start instead of only 1, so we end up with 3 frames longer vid than audio. Maybe we found another bug in Fadein Processor here.
As a result, the ffmpeg filter apad gets some work to do and only in this case we get some odd audio output duration (+5ms and co.).

Potentially @movalex can workaround by replacing the Fade processor with a custom avs script:

Code: Select all

audio = m_clip
video = m_clip
video = Fadein(video, Round(6))
video = Fadeout(video, Round(6))
video = Trim(video, 2, video.FrameCount-2)
m_clip = AudioDub(video, audio)
return m_clip
NAEFIR SD DV_2_emcodem.json
(17.6 KiB) Downloaded 62 times
This compensates the issue by cutting only the video and ending up in the avs script outputting matching audio and video duration. This in return does not trigger ffmpeg apad filter so everything is fine. Also we preserve the A/V sync this way.



3 bugs in 3 different tools found in a single thread, not bad @movalex :D
I tried reproducing the issue with apad in a way that we can post a ticket in the ffmpeg tracker but it is not easy to force this problem. I found no way to simplify it enough so others can easily reproduce the issue (preferred without using avisynth so it can be done on linux).
Maybe @FranceBB wants to reproduce and check back if the behaviour of Avisynth filter Fadein (to add 2 black frames instead of 1) is a bug really.
emcodem, wrapping since 2009 you got the rhyme?
Post Reply