Skip to content

Instantly share code, notes, and snippets.

@chexov
Created June 20, 2018 18:32
Show Gist options
  • Save chexov/11240ae16fff0393e42e09185658b220 to your computer and use it in GitHub Desktop.
Save chexov/11240ae16fff0393e42e09185658b220 to your computer and use it in GitHub Desktop.

Revisions

  1. chexov created this gist Jun 20, 2018.
    62 changes: 62 additions & 0 deletions ffmpeg-concat-list.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #!/usr/bin/env python

    import sys
    import os

    def ffmpegconcatlist(ts_list, isPlain):
    tslist = []
    for f in ts_list:
    f = f.strip()
    if not f or not os.stat(f).st_size:
    continue
    ts = os.path.basename(f)
    seq = int(ts.replace(".m4s", "").replace("./",""))
    tslist.append((seq,f))

    tslist = sorted(tslist, key=lambda o: o[0])

    result = []
    for o,f in tslist:
    if (isPlain):
    result.append(("{0}".format(f)) )
    else:
    result.append(("file '{0}'".format(f)) )
    return result

    def test():
    data = """
    /a/b/c/d/9.ts
    aa/a/a/a/5.ts
    a/a/a/a/a/a/1.ts
    a/a/a/a/a/a/a/2.ts
    """
    print("\n".join(ffmpegconcatlist(data.splitlines())), False)

    data = """
    9.ts
    5.ts
    1.ts
    2.ts
    10.ts
    1000.ts
    1010.ts
    2010.ts
    3010.ts
    3013.ts
    """
    print("\n".join(ffmpegconcatlist(data.splitlines())), True)

    if __name__ == "__main__":
    #test()
    result = []
    if len(sys.argv) > 1:
    if (sys.argv[1] == "-plain"):
    result = ffmpegconcatlist(sys.stdin.readlines(), True)
    else:
    print "error. incorrect params"
    sys.exit(1)
    else:
    result = ffmpegconcatlist(sys.stdin.readlines(), False)

    if (len(result) > 0):
    print("\n".join(result))