run code on GIF? #191

Open
opened 2022-01-10 07:54:00 +01:00 by zndomination · 4 comments
zndomination commented 2022-01-10 07:54:00 +01:00 (Migrated from github.com)

Hi how do you run the code on a Gif file than a .mp4?

Hi how do you run the code on a Gif file than a .mp4?
DvST8x commented 2022-01-10 15:55:26 +01:00 (Migrated from github.com)

Just change the video path command from .mp4 to .gif to match the name of your file.

Just change the video path command from **.mp4** to **.gif** to match the name of your file.
ramnnv commented 2022-01-10 19:42:25 +01:00 (Migrated from github.com)

or just rename the file changing the .gif to .mp4 that works for me.

or just rename the file changing the .gif to .mp4 that works for me.
netrunner-exe commented 2022-01-10 21:18:37 +01:00 (Migrated from github.com)

If you really want to add GIF function to SimSwap I came up with two ways to implement it. In both ways you need to modify 3 files in util folder - videoswap.py, videoswap_specific.py and videoswap_multispecific.py. You need to find the last line - clips.write_videofile(save_path,audio_codec='aac') in all that files and change it to:

    name = os.path.splitext(save_path)[0]

    if save_path.lower().endswith(('.gif')):
       try:
          clips.write_gif(save_path)
          print('''GIF export was finished sucsessfuly''')
       except BaseException:
          print('''ERROR! Failed to create GIF. Trying export to video...''')
          clips.write_videofile(save_path,audio_codec='aac')
    else:
          clips.write_videofile(save_path,audio_codec='aac')

It will save possibility to export .mp4 and add extra feature - no matter what input file is - mp4 or GIF, if you change the output file extension to .gif - the file will be saved as GIF, if to .mp4 - the output file will be saved as video. In first case, you will take low size GIF but with poor quality, i really dont know how ti increase quality throu moviepy . Second case - if you want better quality - first you need to install ffmpeg by pip install ffmpeg. Then everything is the same as in the first case, only change the last line to:

    name = os.path.splitext(save_path)[0]

    if save_path.lower().endswith(('.gif')):
       try:
          os.system("ffmpeg -y -v -8 -i './temp_results/frame_%07d.jpg' -vf palettegen=max_colors=256 './temp_results/palette.png'")
          os.system("ffmpeg -y -v -8 -f image2 -framerate {} -i './temp_results/frame_%07d.jpg' -i './temp_results/palette.png' -filter_complex 'paletteuse=dither=none' -y '{}.gif'".format(fps, name))
          print('''GIF export was finished sucsessfuly''')
       except BaseException:
          print('''ERROR! Failed to create GIF. Trying export to video...''')
          clips.write_videofile(save_path,audio_codec='aac')
    else:
          clips.write_videofile(save_path,audio_codec='aac')

This will create a GIF of good quality, but possibly a large size.

Example: python test_video_swapsingle.py --crop_size 224 --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/multi_people_1080p.mp4 --output_path ./output/multi_test_swapsingle.gif --temp_path ./temp_results

or python test_video_swapsingle.py --crop_size 224 --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/jim_carrey.gif --output_path ./output/multi_test_swapsingle.gif --temp_path ./temp_results

Tested on the latest version of SimSwap-512, should work on earlier versions too.

If you really want to add GIF function to SimSwap I came up with two ways to implement it. In both ways you need to modify 3 files in `util` folder - `videoswap.py`, `videoswap_specific.py` and `videoswap_multispecific.py`. You need to find the last line - `clips.write_videofile(save_path,audio_codec='aac')` in all that files and change it to: ``` name = os.path.splitext(save_path)[0] if save_path.lower().endswith(('.gif')): try: clips.write_gif(save_path) print('''GIF export was finished sucsessfuly''') except BaseException: print('''ERROR! Failed to create GIF. Trying export to video...''') clips.write_videofile(save_path,audio_codec='aac') else: clips.write_videofile(save_path,audio_codec='aac') ``` It will save possibility to export .mp4 and add extra feature - no matter what input file is - mp4 or GIF, if you change the output file extension to .gif - the file will be saved as GIF, if to .mp4 - the output file will be saved as video. In first case, you will take low size GIF but with poor quality, i really dont know how ti increase quality throu moviepy . Second case - if you want better quality - first you need to install ffmpeg by `pip install ffmpeg`. Then everything is the same as in the first case, only change the last line to: ``` name = os.path.splitext(save_path)[0] if save_path.lower().endswith(('.gif')): try: os.system("ffmpeg -y -v -8 -i './temp_results/frame_%07d.jpg' -vf palettegen=max_colors=256 './temp_results/palette.png'") os.system("ffmpeg -y -v -8 -f image2 -framerate {} -i './temp_results/frame_%07d.jpg' -i './temp_results/palette.png' -filter_complex 'paletteuse=dither=none' -y '{}.gif'".format(fps, name)) print('''GIF export was finished sucsessfuly''') except BaseException: print('''ERROR! Failed to create GIF. Trying export to video...''') clips.write_videofile(save_path,audio_codec='aac') else: clips.write_videofile(save_path,audio_codec='aac') ``` This will create a GIF of good quality, but possibly a large size. Example: `python test_video_swapsingle.py --crop_size 224 --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/multi_people_1080p.mp4 --output_path ./output/multi_test_swapsingle.gif --temp_path ./temp_results` or `python test_video_swapsingle.py --crop_size 224 --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/jim_carrey.gif --output_path ./output/multi_test_swapsingle.gif --temp_path ./temp_results` Tested on the latest version of SimSwap-512, should work on earlier versions too.
instant-high commented 2022-01-10 23:12:59 +01:00 (Migrated from github.com)

I have a slightly different solution to create looping *.webp

1.) First copy ffmpeg.exe to the root directory of simswap

2.) Modify 3 files in util folder - videoswap.py, videoswap_specific.py and videoswap_multispecific.py

At the beginning of these 3 files add following lines add:

import sys
import platform
import subprocess

3.) after the line clips.write_videofile(save_path,codec="libx264",audio_codec='aac')

add following lines (for looping *.webp):

command = 'ffmpeg.exe -y -i ' + save_path + ' -loop 0 -quality 85 ' + save_path + '.webp'
subprocess.call(command, shell=platform.system() != 'Windows')

or

add following lines (for seamless forward/backward looping *.webp):

filter = ' -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" '
command = 'ffmpeg.exe -y -i ' + save_path + ' -loop 0 -quality 85 ' + filter + save_path + '.webp'
subprocess.call(command, shell=platform.system() != 'Windows')

This first creates the *.mp4 video file and then the looping *.webp with the same filename as the video + ".webp"

EDIT:
you need a *.webp capable viewer (I use Tautology Image Viewer) or open the *.webp in chrome or ....

I have a slightly different solution to create looping *.webp 1.) First copy ffmpeg.exe to the root directory of simswap 2.) Modify 3 files in util folder - videoswap.py, videoswap_specific.py and videoswap_multispecific.py At the beginning of these 3 files add following lines add: _import sys_ _import platform_ _import subprocess_ 3.) after the line _clips.write_videofile(save_path,codec="libx264",audio_codec='aac')_ add following lines (for looping *.webp): _command = 'ffmpeg.exe -y -i ' + save_path + ' -loop 0 -quality 85 ' + save_path + '.webp'_ _subprocess.call(command, shell=platform.system() != 'Windows')_ or add following lines (for seamless forward/backward looping *.webp): _filter = ' -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" '_ _command = 'ffmpeg.exe -y -i ' + save_path + ' -loop 0 -quality 85 ' + filter + save_path + '.webp'_ _subprocess.call(command, shell=platform.system() != 'Windows')_ This first creates the *.mp4 video file and then the looping *.webp with the same filename as the video + ".webp" EDIT: you need a *.webp capable viewer (I use Tautology Image Viewer) or open the *.webp in chrome or ....
Sign in to join this conversation.