Search code examples
pythonpython-3.xsubprocesspopenexiftool

Subprocess does not execute exiftool command


Iam trying to execute an exif command using subprocess. The command is :

['exiftool', '-ID3:Picture', '-b', '-ThumbnailImage', '/home/mediaworker/Downloads/Raabta.mp3', '>', '/mnt/share_PROXY/exifData/Raabta.jpg']

Now, the issue is that it returns the status code as 1. But if i execute the same command in the terminal, it executes successfully. The file is written to the location. Is my command going wrong in subprocess ? The error i get when i run my python script is :

Error: File not found - >
Error: File not found - /mnt/share_PROXY/exifData/Raabta.jpg

The code implementation is as follows:

file_name = os.path.basename(file_loc)
file_name = file_name.replace(os.path.splitext(file_name)[1], ".jpg")
dst_loc = os.path.join(dst_loc, file_name)
cmd_ = ["exiftool", "-ID3:Picture", "-b", "-ThumbnailImage", file_loc, ">", dst_loc]
logger.info("Command is {}".format(cmd_))
    try:
        p = subprocess.Popen(cmd_, stdout=subprocess.PIPE)
        p.communicate()
        if p.returncode != 0:
            logger.error("Failed to write thumbnail artwork")
        else:                          
            id3_metadata.append({"file_thumbnail_info_path": dst_loc})
    except Exception:
        logger.error("[extract_iptc_metadata] Exception : '{}'".format(ex))

Solution

  • The error output refers to the redirection >.

    The proper way to redirect using subprocess is using the stdout parameter.

        with open(dst_loc, 'wb') as f:
            p = subprocess.Popen(cmd_, stdout=f)
            p.communicate()