import asyncio


async def findVideoRotation(fileName):
    """Find video rotation using ffprobe."""
    angle = 0
    ffprobe_cmd = "ffprobe -v quiet -select_streams v:0 -show_entries " \
                  "stream_tags=rotate -of default=nw=1:nk=1".split(' ')
    ffprobe_cmd.append(fileName)
    try:
        proc = await asyncio.create_subprocess_exec(*ffprobe_cmd,
                                                    stdout=asyncio.subprocess.PIPE)
        op, err = await proc.communicate()
        op = op.decode()
        for line in op.split('\n'):
            if "90" in line:
                angle = 90
            elif "180" in line:
                angle = 180
            elif "270" in line:
                angle = 270
    except OSError:
        # Likely couldn't find ffprobe
        pass

    if angle < 0:
        angle = angle + 360

    return angle