[MPlayer-dev-eng] [PATCH] Subtitles sorting

Salvatore Falco sfalco at studenti.ing.uniroma1.it
Mon Nov 11 19:01:21 CET 2002


    Hi all.

    This small patch is to sort subtitles.
    Some subtitle files I found have unsorted subtitles, with some
subtitles put after (timely) following ones.
    Sorting is done when loading the subtitles from the file, with a
linear search for the position, since we can safetly assume that many,
if not all, subtitles are correctly ordered.
    This patch also supports those subtitle formats, such as ARQ and
Subrip09, which store the end of a subtitle as the beginning of the
following.

    Bye, Salvatore Falco
-------------- next part --------------
LS0tIHN1YnJlYWRlci5jLm9yaWcJVGh1IE5vdiAgNyAwMDo1NDoyMSAyMDAyCisrKyBzdWJyZWFk
ZXIuYwlNb24gTm92IDExIDE3OjU1OjU3IDIwMDIKQEAgLTM1LDYgKzM1LDcgQEAKIAogLyogVXNl
IHRoZSBTVUJfKiBjb25zdGFudCBkZWZpbmVkIGluIHRoZSBoZWFkZXIgZmlsZSAqLwogaW50IHN1
Yl9mb3JtYXQ9U1VCX0lOVkFMSUQ7Cit1bnNpZ25lZCBsb25nIHByZXZpb3VzX3N1Yl9lbmQ7CiAK
IHN0YXRpYyBpbnQgZW9sKGNoYXIgcCkgewogICAgIHJldHVybiAocD09J1xyJyB8fCBwPT0nXG4n
IHx8IHA9PSdcMCcpOwpAQCAtNTEwLDggKzUxMSw2IEBACiAJcmV0dXJuIE5VTEw7IC8vIHdlIHNo
b3VsZCBoYXZlIHJldHVybmVkIGJlZm9yZSBpZiBpdCdzIE9LCiB9CiAKLXN1YnRpdGxlICpwcmV2
aW91c19hcXRfc3ViID0gTlVMTDsKLQogc3VidGl0bGUgKnN1Yl9yZWFkX2xpbmVfYXF0KEZJTEUg
KmZkLHN1YnRpdGxlICpjdXJyZW50KSB7CiAgICAgY2hhciBsaW5lW0xJTkVfTEVOKzFdOwogICAg
IGNoYXIgKm5leHQ7CkBAIC01MjUsMTAgKzUyNCw3IEBACiAJCWJyZWFrOwogICAgIH0KICAgICAK
LSAgICBpZiAocHJldmlvdXNf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lyc3Rbal0ubGluZXMgPSBzdWItPmxpbmVzOworCSAgICAJICAgIGZvcihpID0gMDsgaSA8
IFNVQl9NQVhfVEVYVDsgKytpKXsKKwkJCWZpcnN0W2pdLnRleHRbaV0gPSBzdWItPnRleHRbaV07
CisJCSAgICB9CisJCSAgICBpZiAocHJldmlvdXNfc3ViX2VuZCl7CisJCQlmaXJzdFtqXS5lbmQg
PSBmaXJzdFtqIC0gMV0uZW5kOworCQkJZmlyc3RbaiAtIDFdLmVuZCA9IHByZXZpb3VzX3N1Yl9l
bmQ7CisJCQlwcmV2aW91c19zdWJfZW5kID0gMDsKKwkJICAgIH0KKwkJICAgIGJyZWFrOworICAg
IAkJfQorCSAgICB9CisJfQogICAgICAgICBpZihzdWI9PUVSUikgKytzdWJfZXJyczsgZWxzZSAr
K3N1Yl9udW07IC8vIEVycm9yIHZzLiBWYWxpZAogICAgIH0KICAgICAK


More information about the MPlayer-dev-eng mailing list