tamaina / venc2

https://venc2.vercel.app
MIT License
3 stars 0 forks source link

Total playback time is abnormally long when running in Safari / Safariで処理すると総再生時間が変 #1

Closed tamaina closed 1 year ago

tamaina commented 1 year ago

However Chrome-processed videos don't increase total playback time.

image

image

tamaina commented 1 year ago

https://github.com/tamaina/venc2/assets/7973572/508fbf5f-a510-4d98-9256-6cf1506f89e4

Safariで作ったmp4

tamaina commented 1 year ago

ffmpegはmfra/tfra boxをつけているので、mfra/tfra boxを追加すればうまいこといきそうではあるがmp4boxを改造する必要がある

image

tamaina commented 1 year ago

とりあえずIssueを出してみた https://github.com/gpac/mp4box.js/issues/355

tamaina commented 1 year ago

でもworkerではない処理系の時は普通に時間合ってたんだよな

tamaina commented 1 year ago

でもworkerではない処理系の時は普通に時間合ってたんだよな

でもなんか異常なので今の実装の方が正しいのかも

tamaina commented 1 year ago

(ところで: Chromeで処理した動画が2倍で済んでてSafariで処理した動画はめっちゃ長い理由を理解していない)

tamaina commented 1 year ago

MEMO: https://github.com/Dash-Industry-Forum/codem-isoboxer/blob/master/src/processors/tfra.js

tamaina commented 1 year ago

mfra/tfra/mfroを書いてみたけど解決しなかった

tamaina commented 1 year ago

image

ffmpeg/ffprobeも同じような時間を出してくる

tamaina commented 1 year ago

ffmpegのソースを読めば原因が分かりそうだけどパッと検索した限り端緒を見つけられなかった

tamaina commented 1 year ago

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/dump.c#L644-L669

ここら辺かしら

tamaina commented 1 year ago

durationの触り始め https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/demux.c#L295

tamaina commented 1 year ago

ここ

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/demux.c#L1660

tamaina commented 1 year ago

こっちかも

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/demux.c#L1560
https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/demux.c#L1671

tamaina commented 1 year ago

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/demux.c#L1619

av_rescale_qが変なんかな

tamaina commented 1 year ago

While processing ffprobe, open_input_file() calls avformat_find_stream_info() to correct information then calls av_dump_format() to output.

avformat_find_stream_info() calls estimate_timings().

estimate_timings() is most relevant for calculating duration, conditionally branching and calling lower level functions.

tamaina commented 1 year ago

r_frame_rateがおかしくて、これによって色々ダメなことが起きてるらしい

tamaina commented 1 year ago

ログによれば max_analyze_duration に引っかかってるっぽい

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavformat/demux.c#L2730

tamaina commented 1 year ago

いや結局それってtime_baseがおかしいので(?)ffmpegの内部durationが滅茶苦茶になっているだけだよな

tamaina commented 1 year ago

VideoEncoderで指定してやらないとdescriptionのSPSのフレームレートが変になって、そいでもってtime_baseがおかしくなるのか

tamaina commented 1 year ago

残念ながらVideoEncoderにframerateを設定しても変わらん

tamaina commented 1 year ago

avcDecoderConfigRecord Safari vs Chrome

image

tamaina commented 1 year ago

(最後のPPSが変わってないからいいじゃんと勘違いしていたけど、関係あるのは真ん中のSPSだったわ)

tamaina commented 1 year ago

SPSのパース

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavcodec/h264_ps.c#L142-L158

timing_info_present_flagがあったらここでframerateとして記録している?

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavcodec/h264_parser.c#L570-L571

tamaina commented 1 year ago

timing_info_present_flagがあったらここでframerateとして記録している?

av_reduceは引数2, 3を約分し(reduce)て引数0, 1(ポインタ渡し)に代入するという感じらしい

tamaina commented 1 year ago

https://wikiwiki.jp/redstrange/H.264

tamaina commented 1 year ago

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavcodec/h264_ps.c#L560

tamaina commented 1 year ago

https://github.com/FFmpeg/FFmpeg/blob/fa20f5cd9e131f22da06ef57bf5aedd87ff51a90/libavcodec/h264_ps.c#L560

これの起動の仕方に15分悩んでたけどコマンドに-debug pictを付け足せばいけた

tamaina commented 1 year ago

memo

ffprobe target.mp4  -hide_banner -loglevel warning -loglevel info -loglevel verbose -loglevel debug -debug pict -of default -show_streams

image

tamaina commented 1 year ago

r_frame_rateは主にff_rfps_calculateが決定してそうだけど解読が億劫

tamaina commented 1 year ago

睡眠を経て開いたら何をやってたかわからなくなったが結局SPSをいじってnum_units_in_tick/time_scaleを突っ込むのが手っ取り早い気がしていたのは覚えている

tamaina commented 1 year ago

ChatGPTに聞いてみた

そんなコマンドあるんだ

image

tamaina commented 1 year ago

SPSをいじらなくてもVideoEncoderのconfig.decoderConfig.descriptionがtiming_info_present_flag=1にしてくれれば済む件

tamaina commented 1 year ago

つかedts/elstのmedia_timeを参考にしてくれれば悩むことないんだけどそういうふうに仕向けられないかしら

Mr-Ojii commented 1 year ago

少し調べてみたところ、こちらはtransform.tsで最終フレーム付近のフレームのソートが正常にできていないことが根本的な問題のようです

tamaina commented 1 year ago

エンコードした後のtimestampが順番通りでなく出てきてるんだわこれ

tamaina commented 1 year ago

EncodedVideoChunkはソートしてはいけないのでうーん

tamaina commented 1 year ago

durationをsortの時点で付与するかしら

tamaina commented 1 year ago

transform.tsで最終フレーム付近のフレームのソートが正常にできていない

ばちくそ根本的な処理がまちがってました…