SimSwap icon indicating copy to clipboard operation
SimSwap copied to clipboard

run code on GIF?

Open zndomination opened this issue 3 years ago • 4 comments

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

zndomination avatar Jan 10 '22 06:01 zndomination

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

DvST8x avatar Jan 10 '22 14:01 DvST8x

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

ramnnv avatar Jan 10 '22 18:01 ramnnv

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.

netrunner-exe avatar Jan 10 '22 20:01 netrunner-exe

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 ....

instant-high avatar Jan 10 '22 22:01 instant-high