Skip to content

Instantly share code, notes, and snippets.

@rock3r
Last active January 14, 2022 09:00
Show Gist options
  • Select an option

  • Save rock3r/a923a79e8d8a850911aa to your computer and use it in GitHub Desktop.

Select an option

Save rock3r/a923a79e8d8a850911aa to your computer and use it in GitHub Desktop.

Revisions

  1. rock3r revised this gist Oct 10, 2016. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -17,13 +17,14 @@
    def look_for_ffmpeg_or_abort():
    ffmpeg_path = find_executable('ffmpeg')
    if ffmpeg_path == None:
    print("** ComputerSaysNoError **")
    print("You need to have ffmpeg installed on your system and on the path for Giffify to work" )
    print "** ComputerSaysNoError **"
    print "You need to have ffmpeg installed on your system and on the path for Giffify to work"
    exit(1)

    def parse_cli_arguments():
    parser = argparse.ArgumentParser(description='Processes a video into a gif.')
    parser.add_argument('video', type=str, help='The video to be processed')
    parser.add_argument('-r', '--rotate', dest='rotate', action='store_true', help='Rotate output 270 degrees clockwise (useful for Genymotion)')
    parser.add_argument('-o', '--outfile', type=str, help='Target path')
    parser.add_argument('-dw', '--desired-width', type=int, default=-1)
    parser.add_argument('-dh', '--desired-height', type=int, default=-1)
    @@ -65,10 +66,15 @@ def insert_before_output_path(args, elements):
    e = args.end_time
    d = args.duration

    if args.rotate:
    rotate_filters = "transpose=2,"
    else:
    rotate_filters = ""

    filters = "fps={fps},scale={dw}:{dh}:flags=lanczos".format(fps = fps, dw = dw, dh = dh)
    palette_filters = "{filters},palettegen".format(filters = filters)
    output_filters = "{filters} [x]; [x][1:v] paletteuse".format(filters = filters)
    output_filters = "{rotate}{filters} [x]; [x][1:v] paletteuse".format(filters = filters, rotate = rotate_filters)

    palette_filters = "{rotate}{filters},palettegen".format(filters = filters, rotate = rotate_filters)
    palette_path = get_palette_path()

    ffmpeg_args_palette = ['ffmpeg',
    @@ -92,10 +98,10 @@ def insert_before_output_path(args, elements):
    if d != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-t", str(d)])

    print("First pass: extracting colour palette, hang tight...")
    print "First pass: extracting colour palette, hang tight..."
    subprocess.call(ffmpeg_args_palette)

    print("Second pass: converting that nice video into a sweet, high quality gif...")
    print "Second pass: converting that nice video into a sweet, high quality gif..."
    subprocess.call(ffmpeg_args_gif)

    print("Done! Now go and show off to your friends and colleagues")
    print "Done! Now go and show off to your friends and colleagues"
  2. rock3r revised this gist Aug 5, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@
    def look_for_ffmpeg_or_abort():
    ffmpeg_path = find_executable('ffmpeg')
    if ffmpeg_path == None:
    print "** ComputerSaysNoError **"
    print "You need to have ffmpeg installed on your system and on the path for Giffify to work"
    print("** ComputerSaysNoError **")
    print("You need to have ffmpeg installed on your system and on the path for Giffify to work" )
    exit(1)

    def parse_cli_arguments():
    @@ -92,10 +92,10 @@ def insert_before_output_path(args, elements):
    if d != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-t", str(d)])

    print "First pass: extracting colour palette, hang tight..."
    print("First pass: extracting colour palette, hang tight...")
    subprocess.call(ffmpeg_args_palette)

    print "Second pass: converting that nice video into a sweet, high quality gif..."
    print("Second pass: converting that nice video into a sweet, high quality gif...")
    subprocess.call(ffmpeg_args_gif)

    print "Done! Now go and show off to your friends and colleagues"
    print("Done! Now go and show off to your friends and colleagues")
  3. rock3r revised this gist Mar 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion giffify.sh
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    # this notice you can do whatever you want with this stuff. If we meet some day,
    # and you think this stuff is worth it, you can buy us a beer in return.

    if ! hash "ffmpeg" > /dev/null 2>&1; then
    if ! which "ffmpeg" > /dev/null 2>&1; then
    echo "You need ffmpeg in your path to run giffify" >&2
    exit 1
    fi
  4. rock3r revised this gist Mar 3, 2016. 2 changed files with 78 additions and 0 deletions.
    1 change: 1 addition & 0 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/usr/bin/python

    # License for any modification to the original (linked below):
    # ----------------------------------------------------------------------------
    # "THE BEER-WARE LICENSE" (Revision 42):
    77 changes: 77 additions & 0 deletions giffify.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/bin/bash

    # License for any modification to the original (linked below):
    # ----------------------------------------------------------------------------
    # "THE BEER-WARE LICENSE" (Revision 42):
    # Sebastiano Poggi and Daniele Conti wrote this file. As long as you retain
    # this notice you can do whatever you want with this stuff. If we meet some day,
    # and you think this stuff is worth it, you can buy us a beer in return.

    if ! hash "ffmpeg" > /dev/null 2>&1; then
    echo "You need ffmpeg in your path to run giffify" >&2
    exit 1
    fi

    show_usage() {
    echo "Usage:"
    echo ""
    echo "giffify -i input_path.mp4 -o output_path.gif [-w width] [-y height] [-f fps=15] [-h shows this message]"
    }

    input_path=""
    output_path=""
    dw=-1
    dh=-1
    fps=15

    while getopts ":i:o:w:y:f:h" opt; do
    case $opt in
    i )
    input_path=$OPTARG
    ;;
    o )
    output_path=$OPTARG
    ;;
    w )
    dw=$OPTARG
    ;;
    y )
    dh=$OPTARG
    ;;
    f )
    fps=$OPTARG
    ;;
    h )
    show_usage
    exit 0
    ;;
    \? )
    echo "Argument $OPTARG not recognized." >&2
    exit 1
    ;;
    : )
    echo "Option -$OPTARG requires an argument." >&2
    exit 1
    ;;
    esac
    done

    if [[ -z "$input_path" ]]; then
    echo "You must specify an input file to process"
    echo ""
    show_usage
    exit 1
    fi

    if [[ -z "$output_path" ]]; then
    output_path="$input_path.gif"
    fi

    palette="palette.png"
    filters="fps=$fps,scale=$dw:$dh:flags=lanczos"

    ffmpeg -v warning -i $input_path -vf "$filters,palettegen" -y $palette
    if [[ -f $palette ]]; then
    ffmpeg -v warning -i $input_path -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $output_path
    rm $palette
    fi
  5. rock3r revised this gist Mar 3, 2016. 1 changed file with 38 additions and 17 deletions.
    55 changes: 38 additions & 17 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,30 @@

    import argparse, sys, subprocess, tempfile
    from os.path import splitext
    from distutils.spawn import find_executable




    def look_for_ffmpeg_or_abort():
    ffmpeg_path = find_executable('ffmpeg')
    if ffmpeg_path == None:
    print "** ComputerSaysNoError **"
    print "You need to have ffmpeg installed on your system and on the path for Giffify to work"
    exit(1)

    def parse_cli_arguments():
    parser = argparse.ArgumentParser(description='Processes a video into a gif.')
    parser.add_argument('video', type=str, help='The video to be processed')
    parser.add_argument('-o', '--outfile', type=str, help='Target path')
    parser.add_argument('-dw', '--desired-width', type=int, default=-1)
    parser.add_argument('-dh', '--desired-height', type=int, default=-1)
    parser.add_argument('-fps', type=int, default=15, help='Output frames per second. Default: 15 fps')
    parser.add_argument('-s', '--start-time', type=int, default=-1, help='Start timestamp, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]')
    parser.add_argument('-e', '--end-time', type=int, default=-1, help='End timestamp, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]. Overridden by -d')
    parser.add_argument('-d', '--duration', type=int, default=-1, help='Duration, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]. Overrides -e')

    return parser.parse_args()

    def gif_path(path):
    return splitext(path)[0] + '.gif'
    @@ -23,34 +47,26 @@ def insert_before_output_path(args, elements):
    index = args.index('-y')
    return args[:index] + elements + args[index:]

    parser = argparse.ArgumentParser(description='Processes a video into a gif.')
    parser.add_argument('video', type=str, help='The video to be processed')
    parser.add_argument('-o', '--outfile', type=str, help='Target path')
    parser.add_argument('-dw', '--desired-width', type=int, default=-1)
    parser.add_argument('-dh', '--desired-height', type=int, default=-1)
    parser.add_argument('-fps', type=int, default=15, help='Output frames per second. Default: 15 fps')
    parser.add_argument('-s', '--start-time', type=int, default=-1, help='Start timestamp, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]')
    parser.add_argument('-e', '--end-time', type=int, default=-1, help='End timestamp, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]. Overridden by -d')
    parser.add_argument('-d', '--duration', type=int, default=-1, help='Duration, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]. Overrides -e')

    args = parser.parse_args()
    look_for_ffmpeg_or_abort()

    input_path = args.video
    args = parse_cli_arguments()

    input_path = args.video
    output_path = gif_path(input_path) if args.outfile is None else args.outfile

    fps = args.fps

    dw = args.desired_width
    dh = args.desired_height

    s=args.start_time
    e=args.end_time
    d=args.duration
    s = args.start_time
    e = args.end_time
    d = args.duration

    filters="fps={fps},scale={dw}:{dh}:flags=lanczos".format(fps=fps, dw=dw, dh=dh)
    palette_filters="{filters},palettegen".format(filters=filters)
    output_filters="{filters} [x]; [x][1:v] paletteuse".format(filters=filters)
    filters = "fps={fps},scale={dw}:{dh}:flags=lanczos".format(fps = fps, dw = dw, dh = dh)
    palette_filters = "{filters},palettegen".format(filters = filters)
    output_filters = "{filters} [x]; [x][1:v] paletteuse".format(filters = filters)

    palette_path = get_palette_path()

    @@ -75,5 +91,10 @@ def insert_before_output_path(args, elements):
    if d != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-t", str(d)])

    print "First pass: extracting colour palette, hang tight..."
    subprocess.call(ffmpeg_args_palette)

    print "Second pass: converting that nice video into a sweet, high quality gif..."
    subprocess.call(ffmpeg_args_gif)

    print "Done! Now go and show off to your friends and colleagues"
  6. rock3r revised this gist Mar 3, 2016. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -75,8 +75,5 @@ def insert_before_output_path(args, elements):
    if d != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-t", str(d)])

    print(ffmpeg_args_palette)
    print(ffmpeg_args_gif)

    subprocess.call(ffmpeg_args_palette)
    subprocess.call(ffmpeg_args_gif)
  7. rock3r revised this gist Mar 3, 2016. 1 changed file with 38 additions and 3 deletions.
    41 changes: 38 additions & 3 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -19,12 +19,19 @@ def get_palette_path():
    finally:
    palette_file.close()

    def insert_before_output_path(args, elements):
    index = args.index('-y')
    return args[:index] + elements + args[index:]

    parser = argparse.ArgumentParser(description='Processes a video into a gif.')
    parser.add_argument('video', type=str, help='The video to be processed')
    parser.add_argument('-o', '--outfile', type=str, help='Target path')
    parser.add_argument('-dw', '--desired-width', type=int, default=-1)
    parser.add_argument('-dh', '--desired-height', type=int, default=-1)
    parser.add_argument('-fps', type=int, default=15)
    parser.add_argument('-fps', type=int, default=15, help='Output frames per second. Default: 15 fps')
    parser.add_argument('-s', '--start-time', type=int, default=-1, help='Start timestamp, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]')
    parser.add_argument('-e', '--end-time', type=int, default=-1, help='End timestamp, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]. Overridden by -d')
    parser.add_argument('-d', '--duration', type=int, default=-1, help='Duration, as [-][HH:]MM:SS[.m...] or [-]S+[.m...]. Overrides -e')

    args = parser.parse_args()

    @@ -37,11 +44,39 @@ def get_palette_path():
    dw = args.desired_width
    dh = args.desired_height

    s=args.start_time
    e=args.end_time
    d=args.duration

    filters="fps={fps},scale={dw}:{dh}:flags=lanczos".format(fps=fps, dw=dw, dh=dh)
    palette_filters="{filters},palettegen".format(filters=filters)
    output_filters="{filters} [x]; [x][1:v] paletteuse".format(filters=filters)

    palette_path = get_palette_path()

    subprocess.call(['ffmpeg', '-v', 'warning', '-i', input_path, '-vf', palette_filters, '-y', palette_path])
    subprocess.call(['ffmpeg', '-v', 'warning', '-i', input_path, '-i', palette_path, '-lavfi', output_filters, '-y', output_path])
    ffmpeg_args_palette = ['ffmpeg',
    '-v', 'warning',
    '-i', input_path,
    '-vf', palette_filters,
    '-y', palette_path]
    ffmpeg_args_gif = ['ffmpeg',
    '-v', 'warning',
    '-i', input_path,
    '-i', palette_path,
    '-lavfi', output_filters,
    '-y', output_path]

    if s != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-ss", str(s)])

    if e != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-to", str(e)])

    if d != -1:
    ffmpeg_args_gif = insert_before_output_path(ffmpeg_args_gif, ["-t", str(d)])

    print(ffmpeg_args_palette)
    print(ffmpeg_args_gif)

    subprocess.call(ffmpeg_args_palette)
    subprocess.call(ffmpeg_args_gif)
  8. rock3r revised this gist Jan 8, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@
    # this notice you can do whatever you want with this stuff. If we meet some day,
    # and you think this stuff is worth it, you can buy us a beer in return.


    import argparse, sys, subprocess, tempfile
    from os.path import splitext

    @@ -42,7 +41,6 @@ def get_palette_path():
    palette_filters="{filters},palettegen".format(filters=filters)
    output_filters="{filters} [x]; [x][1:v] paletteuse".format(filters=filters)


    palette_path = get_palette_path()

    subprocess.call(['ffmpeg', '-v', 'warning', '-i', input_path, '-vf', palette_filters, '-y', palette_path])
  9. rock3r revised this gist Jan 8, 2016. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -38,10 +38,6 @@ def get_palette_path():
    dw = args.desired_width
    dh = args.desired_height

    if dw is -1 and dh is -1:
    print >>sys.stderr, "You must choose at least one between height and width"
    exit(1)

    filters="fps={fps},scale={dw}:{dh}:flags=lanczos".format(fps=fps, dw=dw, dh=dh)
    palette_filters="{filters},palettegen".format(filters=filters)
    output_filters="{filters} [x]; [x][1:v] paletteuse".format(filters=filters)
  10. rock3r revised this gist Jan 8, 2016. 2 changed files with 53 additions and 32 deletions.
    53 changes: 53 additions & 0 deletions giffify.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/python
    # License for any modification to the original (linked below):
    # ----------------------------------------------------------------------------
    # "THE BEER-WARE LICENSE" (Revision 42):
    # Sebastiano Poggi and Daniele Conti wrote this file. As long as you retain
    # this notice you can do whatever you want with this stuff. If we meet some day,
    # and you think this stuff is worth it, you can buy us a beer in return.


    import argparse, sys, subprocess, tempfile
    from os.path import splitext

    def gif_path(path):
    return splitext(path)[0] + '.gif'

    def get_palette_path():
    try:
    palette_file = tempfile.NamedTemporaryFile()
    return palette_file.name + '.png'
    finally:
    palette_file.close()

    parser = argparse.ArgumentParser(description='Processes a video into a gif.')
    parser.add_argument('video', type=str, help='The video to be processed')
    parser.add_argument('-o', '--outfile', type=str, help='Target path')
    parser.add_argument('-dw', '--desired-width', type=int, default=-1)
    parser.add_argument('-dh', '--desired-height', type=int, default=-1)
    parser.add_argument('-fps', type=int, default=15)

    args = parser.parse_args()

    input_path = args.video

    output_path = gif_path(input_path) if args.outfile is None else args.outfile

    fps = args.fps

    dw = args.desired_width
    dh = args.desired_height

    if dw is -1 and dh is -1:
    print >>sys.stderr, "You must choose at least one between height and width"
    exit(1)

    filters="fps={fps},scale={dw}:{dh}:flags=lanczos".format(fps=fps, dw=dw, dh=dh)
    palette_filters="{filters},palettegen".format(filters=filters)
    output_filters="{filters} [x]; [x][1:v] paletteuse".format(filters=filters)


    palette_path = get_palette_path()

    subprocess.call(['ffmpeg', '-v', 'warning', '-i', input_path, '-vf', palette_filters, '-y', palette_path])
    subprocess.call(['ffmpeg', '-v', 'warning', '-i', input_path, '-i', palette_path, '-lavfi', output_filters, '-y', output_path])
    32 changes: 0 additions & 32 deletions giffify.sh
    Original file line number Diff line number Diff line change
    @@ -1,32 +0,0 @@
    #!/bin/sh
    # License for any modification to the original (linked below):
    # ----------------------------------------------------------------------------
    # "THE BEER-WARE LICENSE" (Revision 42):
    # Sebastiano Poggi wrote this file. As long as you retain this notice you
    # can do whatever you want with this stuff. If we meet some day, and you think
    # this stuff is worth it, you can buy me a beer in return.
    # ----------------------------------------------------------------------------
    #
    # Based upon http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
    # Usage: giffify.sh inputFile outputFile [targetHeight] [targetFps]
    # Defaults: defaultHeight = 500px, targetFps = 15

    inputFile="$1"
    outputFile="$2"
    desiredHeight=$3
    if [[ -z $desiredHeight ]]; then
    desiredHeight=500
    fi
    fps=$4
    if [[ -z $fps ]]; then
    fps=15
    fi

    palette="palette.png"
    filters="fps=$fps,scale=-1:$desiredHeight:flags=lanczos"

    ffmpeg -v warning -i $inputFile -vf "$filters,palettegen" -y $palette
    if [[ -f $palette ]]; then
    ffmpeg -v warning -i $inputFile -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $outputFile
    rm $palette
    fi
  11. rock3r revised this gist Jul 4, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions giffify.sh
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@
    # License for any modification to the original (linked below):
    # ----------------------------------------------------------------------------
    # "THE BEER-WARE LICENSE" (Revision 42):
    # <[email protected]> wrote this file. As long as you retain this notice you
    # Sebastiano Poggi wrote this file. As long as you retain this notice you
    # can do whatever you want with this stuff. If we meet some day, and you think
    # this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
    # this stuff is worth it, you can buy me a beer in return.
    # ----------------------------------------------------------------------------
    #
    # Based upon http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
  12. rock3r revised this gist Jul 3, 2015. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion giffify.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    #!/bin/sh
    # Originally from http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
    # License for any modification to the original (linked below):
    # ----------------------------------------------------------------------------
    # "THE BEER-WARE LICENSE" (Revision 42):
    # <[email protected]> wrote this file. As long as you retain this notice you
    # can do whatever you want with this stuff. If we meet some day, and you think
    # this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
    # ----------------------------------------------------------------------------
    #
    # Based upon http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
    # Usage: giffify.sh inputFile outputFile [targetHeight] [targetFps]
    # Defaults: defaultHeight = 500px, targetFps = 15

  13. rock3r created this gist Jul 3, 2015.
    24 changes: 24 additions & 0 deletions giffify.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/bin/sh
    # Originally from http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
    # Usage: giffify.sh inputFile outputFile [targetHeight] [targetFps]
    # Defaults: defaultHeight = 500px, targetFps = 15

    inputFile="$1"
    outputFile="$2"
    desiredHeight=$3
    if [[ -z $desiredHeight ]]; then
    desiredHeight=500
    fi
    fps=$4
    if [[ -z $fps ]]; then
    fps=15
    fi

    palette="palette.png"
    filters="fps=$fps,scale=-1:$desiredHeight:flags=lanczos"

    ffmpeg -v warning -i $inputFile -vf "$filters,palettegen" -y $palette
    if [[ -f $palette ]]; then
    ffmpeg -v warning -i $inputFile -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $outputFile
    rm $palette
    fi