Running Python scripts on server over ssh and getting back content

Running Python scripts on server over ssh and getting back content

June 17, 2021

Context: I upload GIFs to my blog from the screencasts I create on my iPad. a-shell provides ffmpeg, but, it runs out of memory often.

Solution: Send video to server and run a Python script to convert to GIF and get the content back

I use two scripts:

First called that uses moviepy (FFMPEG under the hood) to convert an MP4 to a GIF

from moviepy.editor import *
import sys

vid = sys.argv[1]
vid_name = vid.split(".")[0]
clip = (VideoFileClip(vid).speedx(10).resize(0.3))
clip.write_gif(vid_name + ".gif")

Second called that accepts: - a video file as a command line argument - transfers the video files to the server using SFTP - transfers the file to the server using SFTP - runs the python file on the server and create the GIF - retreive the GIF back to my local machine

import sys
import paramiko

# The file you wish to convert to GIF
f = sys.argv[1]
f_name_without_ext = f.split(".")[0]
f_name_gif = f_name_without_ext + ".gif"

ssh_client =paramiko.SSHClient()
ssh_client.connect(hostname='MY-HOSTNAME', username='MY-USERNAME', password='MY-PASSWORD')

# Transferring to server
ftp_client.put('', '')

# Transferring video to server
ftp_client.put(f, f)

# Running Python script
stdin, stdout, stderr = ssh_client.exec_command(f"python {f}")
e = stderr.readlines()

# Retreiving the video back from server
ftp_client.get(f_name_gif, f_name_gif)

In another previous post, I had also mentioned about the amazing SSH/SFTP apps: Termius and ShellFish. I am again pasting the GIF showing a similar to above workflow initiated via ShellFish.