neuralchen / SimSwap

An arbitrary face-swapping framework on images and videos with one single trained model!
Other
4.55k stars 895 forks source link

run code on GIF? #191

Open zndomination opened 2 years ago

zndomination commented 2 years ago

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

DvST8x commented 2 years ago

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

ramnnv commented 2 years ago

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

netrunner-exe commented 2 years ago

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 2 years ago

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",audiocodec='aac')

add following lines (for looping *.webp):

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

or

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

_filter = ' -filtercomplex "[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 + savepath + '.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 ....