def embed_video(input_video, watermark_string, output_video): command_read = [ FFMPEG_BIN, '-i', input_video, '-f', 'image2pipe', '-pix_fmt', 'yuv420p', '-vcodec', 'rawvideo', '-' ] pipe_read = sp.Popen(command_read, stdout=sp.PIPE, bufsize=10**8) command_write = [ FFMPEG_BIN, '-y', # (optional) overwrite output file if it exists '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', '1920x1080', '-pix_fmt', 'yuv420p', '-i', '-', # The input comes from a pipe '-q:v', '2', output_video ] pipe_write = sp.Popen(command_write, stdin=sp.PIPE) raw_image = pipe_read.stdout.read(1920 * 1080 * 3) while raw_image != None and len(raw_image) != 0: # transform the byte read into a numpy array image = numpy.fromstring(raw_image, dtype='uint8') image = image.reshape((1080, 1920, 3)) # throw away the data in the pipe's buffer. pipe_read.stdout.flush() img_tmp = image[:1080, :1920, 0] robust.embed_watermark(img_tmp, watermark_string) pipe_write.stdin.write(image.tostring()) raw_image = pipe_read.stdout.read(1920 * 1080 * 3)
def image(): global IMAGE_PATH form = WatermarkForm() if form.validate_on_submit(): f = request.files['file'] save_path = os.path.join(current_app.instance_path, 'upload', f.filename) f.save(save_path) IMAGE_PATH = save_path if len(IMAGE_PATH) == 0: return redirect(url_for('image')) else: watermark_string = form.watermark.data temp_file_path = os.path.join(current_app.instance_path, 'temp', 'temp.jpg') print(temp_file_path) embed_watermark(IMAGE_PATH, watermark_string, temp_file_path) print('ok') return redirect(url_for('image_get', filename='temp.jpg')) #return redirect(url_for('image')) return render_template('image.html', form=form)
'csrf_token': csrf_token } r = s.post(url + '/auth/login', data=params) if not r.status_code == 200: print('Login failed.') return image = Path(file_path) from_data = {'text': text} file = {'image': (image.name, open(image, 'rb'))} r = s.post(url + '/api/posts', data=from_data, files=file) if not r.status_code == 201: print('Upload post failed.') return if __name__ == '__main__': username = '' password = '' text = '' image_file = r'' url = 'http://127.0.0.1:5000' new_image = Path(image_file).with_name('embed.jpg') embed_watermark(image_file, 'hello,world!', str(new_image)) send_post(username, password, text, str(new_image), url)