PyExifTool - Add 360 Metadata to Video File

Started by canjmp, February 12, 2024, 03:28:28 PM

Previous topic - Next topic

canjmp

I wrote a program which creates 'output.mp4' file with 'ffmpeg' and then attempts to add 360 metadata to the 'output.mp4' file. It fails to add the 360 metadata. Any help would be appreciated.

Here is a snippet:

import tkinter as tk
import subprocess
import exiftool


def add_360_metadata():
    # Create an ExifTool instance
    with exiftool.ExifTool() as et:
        # Define the metadata to be added
        metadata = {'XMP-GSpherical:Spherical': 'true'}

        # Join the list of parameters into a single string
        params = ['-json=1', '-overwrite_original', '-XMP-GSpherical:Spherical=true']
        param_string = params + ['output.mp4']


        # Execute the command and print the output
        output = et.execute(*param_string)



Here is the full code:

import tkinter as tk
import subprocess
import exiftool

#This file will play/pause video and render without Confirmation Y/N and without Render Complete OK
file_path = "./short01.mp4"

def add_360_metadata():
    # Create an ExifTool instance
    with exiftool.ExifTool() as et:
        # Define the metadata to be added
        metadata = {'XMP-GSpherical:Spherical': 'true'}

        # Join the list of parameters into a single string
        params = ['-json=1', '-overwrite_original', '-XMP-GSpherical:Spherical=true']
        param_string = params + ['output.mp4']


        # Execute the command and print the output
        output = et.execute(*param_string)


        print('360 metadata added with PyExifTool')


def render_video():
    global file_path

    # Check if a file is loaded
    if not file_path:
        rendering_label.config(font=7, text="No file loaded")
        root.update()
        return

    # Use the loaded file path
    video_path = file_path

    # Define the FFmpeg command using the stored file path
    ffmpeg_command = [
        'ffmpeg',
        '-y',
        '-i', video_path,
        '-vf', 'v360=input=e:yaw=90:output=e',
        '-loglevel', 'info',
        '-progress', 'progress.txt',
        'output.mp4'
    ]

 

    # Execute the FFmpeg command
    subprocess.run(ffmpeg_command)

    add_360_metadata()

    rendering_label.config(font=7, text="Rendering complete")
    root.update()


# Create the main window
root = tk.Tk()
root.title("Render App")

# Create and place the Render button in column 0, row 0
render_button = tk.Button(root, text="Render", command=render_video)
render_button.grid(row=0, column=0, pady=20)

# Create a label for rendering status
rendering_label = tk.Label(root, font=("Arial", 20), text="")
rendering_label.grid(row=10, column=0, pady=20, sticky="s")

# Start the Tkinter event loop
root.mainloop()


Phil Harvey

A breakdown of the problem would be helpful.  Just saying "It fails to add the 360 metadata" isn't very helpful.  Are you using ExifTool to read back this metadata?  Can you write this metadata using ExifTool from the command line?  What command line are you using?  Answering these questions will help narrow down the problem.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

canjmp

#2
After some further reading of topics on this site, I found a correct solution to add 360 metadata to a video file which uses ExifToolHelper. Here is the snippet:

from exiftool import ExifToolHelper
import time
import subprocess

def add_360_metadata(output_file):
    # Create an ExifTool instance
    with ExifToolHelper() as et:
        # Define the XMP metadata to be added
        xmp_metadata = {'XMP-GSpherical:Spherical': 'true'}

        # Set the XMP metadata using set_tags method
        et.set_tags(output_file,
                    tags=xmp_metadata,
                    params=["-P", "-overwrite_original"])



Here is the full code:

from exiftool import ExifToolHelper
import time
import subprocess


#This file will use ffmpeg to Yaw a video 90 deg then add 360 metadata to the output.mp4 file

from exiftool import ExifToolHelper
import time
import subprocess


#This file will use ffmpeg to Yaw a video 90 deg then add 360 metadata to the output.mp4 file
file_path = "./short01.mp4"

def add_360_metadata(output_file):
    # Create an ExifTool instance
    with ExifToolHelper() as et:
        # Define the XMP metadata to be added
        xmp_metadata = {'XMP-GSpherical:Spherical': 'true'}

        # Set the XMP metadata using set_tags method
        et.set_tags(output_file,
                    tags=xmp_metadata,
                    params=["-P", "-overwrite_original"])

    print('360 metadata added with PyExifTool')



def render_video():
    global file_path

    # Check if a file is loaded
    if not file_path:
        print("No file loaded")
        return

    # Use the loaded file path
    video_path = file_path

    # Define the FFmpeg command using the stored file path
    ffmpeg_command = [
        'ffmpeg',
        '-y',
        '-i', video_path,
        '-vf', 'v360=input=e:yaw=90:output=e',
        '-loglevel', 'info',
        '-progress', 'progress.txt',
        'output.mp4'
    ]

    # Execute the FFmpeg command
    subprocess.run(ffmpeg_command)

    add_360_metadata('output.mp4')

    print("Rendering complete")
   
render_video()

canjmp

#3
My lack of basic knowledge of coding, Python and being new to Pyexiftool is why I did not see the solution that was in front of me.

Today, I reread the online documentation Examples at 'https://sylikc.github.io/pyexiftool/examples.html' and see the following:

Setting keywords for a file.

from exiftool import ExifToolHelper
with ExifToolHelper() as et:
    et.set_tags(
        ["rose.jpg", "skyblue.png"],
        tags={"Keywords": ["sunny", "nice day", "cool", "awesome"]}
    )