json 基于API响应的视频自动重命名

vhipe2zx  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(116)

这个问题是来自超级用户的migrated,因为它可以在Stack Overflow. Migrated上回答28天前。
内容:在不小心擦除硬盘后,我设法恢复了所有文件,但它们丢失了所有名称和文件树。因此,我想出了一个自动重命名视频文件的解决方案,尤其是动画视频文件。这个想法是使用trace.moe对每个视频进行截图并提取有关标题,季节和剧集的信息。下面是代码:

import cv2
import requests
import os

def rename_video_with_custom_api(video_path, output_folder):
    try:
        # Ouvrir la vidéo
        video_capture = cv2.VideoCapture(video_path)

        # Avancer la vidéo à 16 minutes (960 secondes)
        video_capture.set(cv2.CAP_PROP_POS_MSEC, 960000)

        # Obtenir le nom de base du fichier vidéo
        base_video_name = os.path.splitext(os.path.basename(video_path))[0]

        # Créer un nom de fichier pour la capture basé sur le nom d'origine du fichier vidéo
        screenshot_filename = f'{base_video_name}_screenshot.jpg'
        screenshot_path = os.path.join(os.path.dirname(video_path), screenshot_filename)

        # Capturer une image à 16 minutes
        ret, frame = video_capture.read()
        video_capture.release()

        if not ret:
            raise Exception("Impossible de capturer l'image à 16 minutes de la vidéo")

        cv2.imwrite(screenshot_path, frame)

        custom_api_url = "https://api.trace.moe/search"
        files = {'image': (screenshot_filename, open(screenshot_path, 'rb'))}
        response = requests.post(custom_api_url, files=files)
        status_code = response.status_code
        response.raise_for_status()

        data = response.json()
        print(data)
        title = data[0].get('title')
        episode = data[0].get('episode')
        season = data[0].get('season')

        if not title or not episode or not season:
            raise Exception("Champs manquants dans la réponse de l'API")

        new_filename = f"{title} - S{season}E{episode}.mp4"

        # Répertoire de destination pour le fichier renommé
        destination_folder = output_folder

        new_filepath = os.path.join(destination_folder, new_filename)

        os.rename(video_path, new_filepath)

        print(f"Renommé {video_path} en {new_filename}")
        print(f"Code d'état de la requête : {status_code}")

    except requests.exceptions.RequestException as e:
        print(f"Erreur lors de la requête API : {str(e)}")
    except Exception as e:
        print(f"Erreur lors du traitement de {video_path} : {str(e)}")

input_folder = 'D:/Projet Reno/Inco'
output_folder = 'D:/Projet Reno/Renommes'  # Le dossier de destination pour les fichiers renommés

for root, dirs, files in os.walk(input_folder):
    for file in files:
        if file.endswith(".MP4"):
            video_path = os.path.join(root, file)
            rename_video_with_custom_api(video_path, output_folder)

字符串
和输出:

{'frameCount': 1623155, 'error': '', 'result': [{'anilist': 143270, 'filename': '[Ohys-Raws] Lycoris Recoil - 12 (BS11 1280x720 x264 AAC).mp4', 'episode': 12, 'from': 958.92, 'to': 967.17, 'similarity': 0.98, 'video': 'https://media.trace.moe/video/143270/%5BOhys-Raws%5D%20Lycoris%20Recoil%20-%2012%20(BS11%201280x720%20x264%20AAC).mp4?t=963.045&now=1697932800&token=gmqiNtCDUDaBmy7ztXIw2YXrRE', 'image': 'https://media.trace.moe/image/143270/%5BOhys-Raws%5D%20Lycoris%20Recoil%20-%2012%20(BS11%201280x720%20x264%20AAC).mp4.jpg?t=963.045&now=1697932800&token=7Tpl7KfmVdqwuERxlR59ZGHDnw'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][24][BIG5][1280X720].mp4', 'episode': 24, 'from': 138, 'to': 138.08, 'similarity': 0.7972284529122763, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B24%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=138.04000000000002&now=1697932800&token=yXfrCVCkLgeFCK8wkbypjvcTpI', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B24%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=138.04000000000002&now=1697932800&token=qNB4kK53Gthr9JS6w5Wick6StsM'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][25][BIG5][1280X720].mp4', 'episode': 25, 'from': 157, 'to': 157.08, 'similarity': 0.7972284529122763, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B25%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=157.04000000000002&now=1697932800&token=9AfLM02dPrLkaXSl5K1Csyx0', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B25%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=157.04000000000002&now=1697932800&token=ZgQlhBIFwpqzrEskqeAgh026AA'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][17][BIG5][1280X720].mp4', 'episode': 17, 'from': 136.92, 'to': 137, 'similarity': 0.7971895450247541, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B17%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=136.95999999999998&now=1697932800&token=YghA2LL8p011eaTwtmgKAliDo0Q', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B17%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=136.95999999999998&now=1697932800&token=BKWaoj32ZfY59GWz26jDLGCGEg'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][14][BIG5][1280X720].mp4', 'episode': 14, 'from': 202.83, 'to': 202.92, 'similarity': 0.7930373526580701, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B14%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=202.875&now=1697932800&token=oCj6LAfuhGCr9mwLAm3pv0OmY', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B14%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=202.875&now=1697932800&token=gbm1TEGBu9mh906sSZl7ENSU5w'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][19][BIG5][1280X720].mp4', 'episode': 19, 'from': 134, 'to': 134.08, 'similarity': 0.79279571675932, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B19%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=134.04000000000002&now=1697932800&token=LcrnciZ6F1TVgwEyrvPdZGLTGaI', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B19%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=134.04000000000002&now=1697932800&token=oLCqY1fHyxJ14goevmzJvDP0'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][16][BIG5][1280X720].mp4', 'episode': 16, 'from': 158.83, 'to': 158.92, 'similarity': 0.7925485288733767, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B16%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=158.875&now=1697932800&token=OFV5hJkJWn7WwDAKD7Qb9q4Sf3I', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B16%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=158.875&now=1697932800&token=8cPsRkUyncSXXXZBuHyeP6zqy0'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][18][BIG5][1280X720].mp4', 'episode': 18, 'from': 134, 'to': 134.08, 'similarity': 0.7921656086712093, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B18%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=134.04000000000002&now=1697932800&token=UkV4iArdhQ2zxavRxFDeEHKokR4', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B18%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=134.04000000000002&now=1697932800&token=7xkQEDqQduYnN1PJ6dFgZGT4ms'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][22][BIG5][1280X720].mp4', 'episode': 22, 'from': 115.08, 'to': 115.17, 'similarity': 0.7910434408948029, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B22%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=115.125&now=1697932800&token=uNVCPVv6zqeQqbtAo5SDcTwYyk', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B22%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=115.125&now=1697932800&token=4af4UneBPS1L2DeMxfaQ251JxJA'}, {'anilist': 20739, 'filename': '[Makari][Gundam Build Fighters Try][23][BIG5][1280X720].mp4', 'episode': 23, 'from': 166.08, 'to': 166.17, 'similarity': 0.7910434408948029, 'video': 'https://media.trace.moe/video/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B23%5D%5BBIG5%5D%5B1280X720%5D.mp4?t=166.125&now=1697932800&token=AjRIfdgzZVdqjiGHrDGouMtfj0', 'image': 'https://media.trace.moe/image/20739/%5BMakari%5D%5BGundam%20Build%20Fighters%20Try%5D%5B23%5D%5BBIG5%5D%5B1280X720%5D.mp4.jpg?t=166.125&now=1697932800&token=8wasBkBtAtDjmEZM0zWelmhUq28'}]}
Erreur lors du traitement de D:/Projet Reno/Inco\36147712.MP4 : 0```


我想指出的是,因为我是一个初学者在这一领域,我可能会去它的错误的方式,但我试图让这段代码的工作,如果我理解正确,错误是在API响应,它似乎不来的形式,一个表,请纠正,如果我是在错误的方式。

bgtovc5b

bgtovc5b1#

看起来data是一个字典(键值对)变量,而你访问它就像一个索引为0的数组。我猜你会想访问result键中的列表值。你只需要在索引之前添加['result']

title = data['result'][0].get('title')
episode = data['result'][0].get('episode')
season = data['result'][0].get('season')

字符串

相关问题