Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

similarity syntax with ffmpeg-python but have different output result #317

Closed
lucemia opened this issue Feb 22, 2024 · 8 comments
Closed
Assignees
Labels
Type: Bug Bug or Bug fixes

Comments

@lucemia
Copy link
Contributor

lucemia commented Feb 22, 2024

env
google colab

case

  1. concat demuxer

1.1. typed-ffmpeg
steps

pip install -U typed-ffmpeg
import ffmpeg
import IPython
(
    ffmpeg
    .input('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4')
    .output(filename = 'output_t.mp4',
            t = 3, )
    .overwrite_output()
    .run()
)
in_file = ffmpeg.input('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4')
ffmpeg.output(in_file.trim(duration = 3),
              in_file.atrim(duration = 3),
              filename = "trim_atrim_duration.mp4",).overwrite_output().run()
f = open('files.txt', "w")
f.write("""
file 'output_t.mp4'
file 'trim_atrim_duration.mp4'
""")
f.close()
(
    ffmpeg
    .input('files.txt', f = 'concat', safe = 0)
    .output(filename = 'concat_demuxer.mp4', c = 'copy')
    .overwrite_output()
    .run()
)
IPython.display.Video('concat_demuxer.mp4', embed = True)

no error occured for video concat_demuxer.mp4
but
the result is not expected
duration is 21 sec,
and
can be play only first 2 sec

expected result
duration is 6 sec (because output_t.mp4 duration is 3 sec
and
trim_atrim_duration.mp4 duration is 3 sec)
and can be play full duration 6 sec

1.2. ffmpeg-python
steps

pip install -U ffmpeg-python
import ffmpeg
import IPython
(
    ffmpeg
    .input('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4')
    .output(filename = 'output_t.mp4',
            t = 3, )
    .overwrite_output()
    .run()
)
in_file = ffmpeg.input('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4')
ffmpeg.output(in_file.trim(duration = 3),
              in_file.filter('atrim', duration = 3),
              filename = "trim_filter_atrim_duration.mp4"
).overwrite_output().run()
f = open('files.txt', "w")
f.write("""
file 'output_t.mp4'
file 'trim_filter_atrim_duration.mp4'
""")
f.close()
(
    ffmpeg
    .input('files.txt', f = 'concat', safe = 0)
    .output(filename = 'concat_demuxer.mp4', c = 'copy')
    .overwrite_output()
    .run()
)
IPython.display.Video('concat_demuxer.mp4', embed = True)

result output video is expected from ffmpeg-python which are 6 secs
and
can be play full

any ideas what's going on in this matter ?

thanks and best regards

Originally posted by @sugizo in #316

https://github.com/livingbio/typed-ffmpeg/discussions/316_
@lucemia lucemia self-assigned this Feb 22, 2024
@lucemia lucemia added the Type: Bug Bug or Bug fixes label Feb 22, 2024
@lucemia
Copy link
Contributor Author

lucemia commented Feb 22, 2024

command run in ffmpeg-python

['ffmpeg', '-i', 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', '-t', '3', 'output_t.mp4', '-y']

['ffmpeg', '-i', 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', '-filter_complex', '[0]trim=duration=3[s0];[0]atrim=duration=3[s1]', '-map', '[s0]', '-map', '[s1]', 'trim_filter_atrim_duration.mp4', '-y']

['ffmpeg', '-f', 'concat', '-safe', '0', '-i', 'files.txt', '-c', 'copy', 'concat_demuxer.mp4', '-y']

@lucemia
Copy link
Contributor Author

lucemia commented Feb 22, 2024

command run in typed-ffmpeg

['ffmpeg', '-y', '-i', 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', '-map', '0', '-t', '3', 'output_t.mp4']

['ffmpeg', '-y', '-i', 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', '-filter_complex', '[0]trim=duration=3[s0];[0]atrim=duration=3[s1]', '-map', '[s0]', '-map', '[s1]', 'trim_atrim_duration.mp4']

['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', 'files.txt', '-map', '0', '-c', 'copy', 'concat_demuxer.mp4']

ffmpeg -y -i http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4 -map 0 -t 3 output_t.mp4

ffmpeg -y -i http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4 -filter_complex '[0]trim=duration=3[s0];[0]atrim=duration=3[s1]' -map '[s0]' -map '[s1]' trim_atrim_duration.mp4

ffmpeg -y -f concat -safe 0 -i files.txt -map 0 -c copy concat_demuxer.mp4

@lucemia
Copy link
Contributor Author

lucemia commented Feb 22, 2024

The -map option in FFmpeg is used to manually select which streams (audio, video, subtitles, etc.) are included in the output file. When you add an extra -map option, you are essentially instructing FFmpeg to include specific streams from the input files into the output. This can lead to differences in the output compared to when no -map option is used, which allows FFmpeg to automatically select the default streams based on its internal logic.

The message:

[mp4 @ 0x15970c350] Non-monotonic DTS in output stream 0:0; previous: 133125, current: 39424; changing to 133126. This may result in incorrect timestamps in the output file.

indicates a problem related to the timestamps of the frames in the output stream. DTS (Decoding Time Stamp) refers to the timestamp that determines when a frame should be decoded. Non-monotonic DTS means that the timestamps are not in a strictly increasing order. This situation can occur for several reasons:

  1. Stream Selection with -map: If you use the -map option to select specific streams, it could result in the exclusion of some streams that were handling timestamp corrections internally. By manually selecting streams, you might disrupt the natural order FFmpeg would typically enforce, leading to non-monotonic timestamps.

  2. Concatenation: When concatenating videos, if the individual clips have different encoding parameters or if their timestamps are not properly aligned, you might end up with non-monotonic DTS errors. This is particularly common when the source files have different frame rates, resolutions, or if there's an error in the way they are concatenated.

  3. Incorrect Input Handling: Sometimes, the source files might have incorrect timestamp information, or there could be an issue with how FFmpeg interprets the timestamps from different streams. This could be exacerbated by the -map option if it's causing FFmpeg to handle streams differently than it would by default.

To solve this issue:

  • Ensure that all video files used in concatenation have compatible formats and frame rates.
  • Consider removing or adjusting the -map options to see if FFmpeg’s default stream selection resolves the timestamp issue.
  • Use the concat filter instead of the concat demuxer if the issue arises during concatenation, as it can handle different frame rates and resolutions more gracefully.
  • If you’re concatenating files, make sure they’re properly aligned and that there are no issues with the source files’ timestamps.

By understanding how the -map option affects stream selection and addressing the potential causes of non-monotonic DTS errors, you should be able to produce a consistent output without incorrect timestamps.

@lucemia
Copy link
Contributor Author

lucemia commented Feb 23, 2024

ffprobe -show_frames -select_streams v:0 -print_format csv trim_with_complex_filter.mp4 | cut -d"," -f5 no use
check PTS

@lucemia
Copy link
Contributor Author

lucemia commented Feb 23, 2024

when run with typed-ffmpeg, in concat demuxer step, there are lot of Non-monotonic DTS in output stream warning
when run with ffmpeg-python, there is only one warning

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x13e406240] Auto-inserting h264_mp4toannexb bitstream filter
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133120, current: 36864; changing to 133121. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133121, current: 37376; changing to 133122. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133122, current: 37888; changing to 133123. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133123, current: 38400; changing to 133124. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133124, current: 38912; changing to 133125. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133125, current: 39424; changing to 133126. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133126, current: 39936; changing to 133127. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133127, current: 40448; changing to 133128. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133128, current: 40960; changing to 133129. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133129, current: 41472; changing to 133130. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133130, current: 41984; changing to 133131. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133131, current: 42496; changing to 133132. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133132, current: 43008; changing to 133133. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133133, current: 43520; changing to 133134. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133134, current: 44032; changing to 133135. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133135, current: 44544; changing to 133136. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133136, current: 45056; changing to 133137. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133137, current: 45568; changing to 133138. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133138, current: 46080; changing to 133139. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133139, current: 46592; changing to 133140. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133140, current: 47104; changing to 133141. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133141, current: 47616; changing to 133142. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133142, current: 48128; changing to 133143. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133143, current: 48640; changing to 133144. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133144, current: 49152; changing to 133145. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133145, current: 49664; changing to 133146. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133146, current: 50176; changing to 133147. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133147, current: 50688; changing to 133148. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133148, current: 51200; changing to 133149. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133149, current: 51712; changing to 133150. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133150, current: 52224; changing to 133151. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133151, current: 52736; changing to 133152. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133152, current: 53248; changing to 133153. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133153, current: 53760; changing to 133154. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133154, current: 54272; changing to 133155. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133155, current: 54784; changing to 133156. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133156, current: 55296; changing to 133157. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133157, current: 55808; changing to 133158. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133158, current: 56320; changing to 133159. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133159, current: 56832; changing to 133160. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133160, current: 57344; changing to 133161. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133161, current: 57856; changing to 133162. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133162, current: 58368; changing to 133163. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133163, current: 58880; changing to 133164. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133164, current: 59392; changing to 133165. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133165, current: 59904; changing to 133166. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133166, current: 60416; changing to 133167. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133167, current: 60928; changing to 133168. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133168, current: 61440; changing to 133169. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133169, current: 61952; changing to 133170. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133170, current: 62464; changing to 133171. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133171, current: 62976; changing to 133172. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133172, current: 63488; changing to 133173. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133173, current: 64000; changing to 133174. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133174, current: 64512; changing to 133175. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133175, current: 65024; changing to 133176. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133176, current: 65536; changing to 133177. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133177, current: 66048; changing to 133178. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133178, current: 66560; changing to 133179. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133179, current: 67072; changing to 133180. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133180, current: 67584; changing to 133181. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133181, current: 68096; changing to 133182. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133182, current: 68608; changing to 133183. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133183, current: 69120; changing to 133184. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133184, current: 69632; changing to 133185. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133185, current: 70144; changing to 133186. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133186, current: 70656; changing to 133187. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133187, current: 71168; changing to 133188. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133188, current: 71680; changing to 133189. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133189, current: 72192; changing to 133190. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133190, current: 72704; changing to 133191. This may result in incorrect timestamps in the output file.
[mp4 @ 0x13e404bc0] Non-monotonic DTS in output stream 0:0; previous: 133191, current: 73216; changing to 133192. This may result in incorrect timestamps in the output file.

@lucemia
Copy link
Contributor Author

lucemia commented Feb 23, 2024

int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
            if (pkt->dts < max) {
                int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
                if (exit_on_error)
                    loglevel = AV_LOG_ERROR;
                av_log(ost, loglevel, "Non-monotonic DTS; "
                       "previous: %"PRId64", current: %"PRId64"; ",
                       ms->last_mux_dts, pkt->dts);
                if (exit_on_error) {
                    return AVERROR(EINVAL);
                }

pkt->dts < max 時會噴的錯

@lucemia
Copy link
Contributor Author

lucemia commented Feb 23, 2024

正確的, current 是 132300
[mp4 @ 0x147714a10] Non-monotonic DTS in output stream 0:1; previous: 133120, current: 132300; changing to 133121. This may result in incorrect timestamps in the output file.

錯誤的, current 是 36864
[mp4 @ 0x14d615970] Non-monotonic DTS in output stream 0:0; previous: 133120, current: 36864; changing to 133121. This may result in incorrect timestamps in the output file.

@lucemia
Copy link
Contributor Author

lucemia commented Feb 23, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug or Bug fixes
Projects
None yet
Development

No branches or pull requests

1 participant