{"id":2908,"date":"2023-02-12T06:49:00","date_gmt":"2023-02-11T21:49:00","guid":{"rendered":"https:\/\/ubun2m.com\/?p=2908"},"modified":"2023-02-13T06:09:04","modified_gmt":"2023-02-12T21:09:04","slug":"%e3%80%90openai%e3%80%91%e6%98%9f%e5%8d%a0%e3%81%84%e3%81%ae%e3%82%b7%e3%83%a7%e3%83%bc%e3%83%88%e5%8b%95%e7%94%bb%e3%82%92%e7%94%9f%e6%88%90%e3%81%99%e3%82%8b%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9","status":"publish","type":"post","link":"https:\/\/ubun2m.com\/?p=2908","title":{"rendered":"\u3010OpenAI\u3011\u661f\u5360\u3044\u306e\u30b7\u30e7\u30fc\u30c8\u52d5\u753b\u3092\u751f\u6210\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3010Python\u3011"},"content":{"rendered":"\n<p>\u3000<a href=\"?p=1635\" title=\"\">\u4ee5\u524d\u306e\u8a18\u4e8b<\/a>\u3067\u3001ChatGPT\u3092\u5229\u7528\u3057\u3066\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u306e\u305f\u3081\u306e\u661f\u5ea7\u5360\u3044\u3092\u4f5c\u6210\u3057\u305f\u3002\u4eca\u56de\u306f\u3001\u661f\u5360\u3044\u306eYoutube\u30b7\u30e7\u30fc\u30c8\u52d5\u753b\u3092\u751f\u6210\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210\u3057\u305f\u306e\u3067\u7d39\u4ecb\u3059\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default m-photo\"><img decoding=\"async\" width=\"512\" height=\"512\" src=\"https:\/\/ubun2m.com\/wp-content\/uploads\/2023\/02\/Attachment.jpg\" alt=\"\" class=\"wp-image-2911\" srcset=\"https:\/\/ubun2m.com\/wp-content\/uploads\/2023\/02\/Attachment.jpg 512w, https:\/\/ubun2m.com\/wp-content\/uploads\/2023\/02\/Attachment-400x400.jpg 400w, https:\/\/ubun2m.com\/wp-content\/uploads\/2023\/02\/Attachment-150x150.jpg 150w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d7\u30ed\u30b0\u30e9\u30e0\u6982\u8981<\/h2>\n\n\n\n<p>\u3000\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001Linux\uff08Ubuntu\uff09\u74b0\u5883\u3067\u52d5\u4f5c\u3059\u308bPython\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3001\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u57fa\u3065\u3044\u3066\u52d5\u753b\u3092\u751f\u6210\u3059\u308b\u3002\u307e\u305f\u3001\u97f3\u58f0\u30c7\u30fc\u30bf\u3082\u542b\u3081\u3066\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u7121\u6599\u306e\u300c<a href=\"https:\/\/voicevox.hiroshiba.jp\/\" title=\"VOICEVOX\">VOICEVOX<\/a>\u300d\u3068\u3044\u3046\u30c6\u30ad\u30b9\u30c8\u8aad\u307f\u4e0a\u3052\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u3092\u4f7f\u3063\u3066\u3044\u308b\u3002\u52d5\u753b\u306fYouTube\u306e\u30b7\u30e7\u30fc\u30c8\u52d5\u753b\u306e\u898f\u683c\u306b\u5408\u308f\u305b\u3001\uff11\u5206\u4ee5\u5185\u306e\u7e26\u9577\u306e\u52d5\u753b\u3068\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u751f\u6210\u3057\u305f\u52d5\u753b\u306e\u30b5\u30f3\u30d7\u30eb\uff08Youtube\u30b7\u30e7\u30fc\u30c8\u52d5\u753b\uff09<\/h2>\n\n\n\n<p>\u3000\u30c9\u30c3\u30c8\u7d75\u3084\u661f\u5360\u3044\u30c7\u30fc\u30bf\u306f\u3001<a href=\"https:\/\/openai.com\/\" title=\"OpenAI\u306eAPI\">OpenAI\u306eAPI<\/a>\u3092\u5229\u7528\u3057\u3066\u4f5c\u6210\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u3010OpenAI\u3011\u4eca\u65e5\u306e\u661f\u5360\u3044\u3092\u81ea\u52d5\u3067\u4f5c\u6210\u3059\u308b\u30c6\u30b9\u30c8\u3010Python\u3011\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/fgM00_XPBHQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30e1\u30a4\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"astrology.py\" data-lang=\"Python\"><code>from PIL import Image, ImageDraw, ImageFont\nfrom moviepy.editor import *\nfrom voicevox import generate_speech\nimport numpy as np\n\n# \u80cc\u666f\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3080\nbackground = Image.open(&#39;bg.jpg&#39;)\n\n# astrology.txt\u3068\u3044\u3046\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\n# txt\u306e\u30c7\u30fc\u30bf\u306e\u5185\u5bb9\uff1aduration\uff08\u518d\u751f\u6642\u9593\uff09,image_file\uff08\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u540d\uff09, text\uff08\u30c6\u30ad\u30b9\u30c8\uff09\nwith open(&#39;astrology.txt&#39;, &#39;r&#39;) as file:\n    # 1\u884c\u305a\u3064\u8aad\u307f\u8fbc\u307f\u3001astrology_text\u3068\u3044\u3046\u30ea\u30b9\u30c8\u306b\u683c\u7d0d\u3059\u308b\n    astrology_text = file.readlines()\n\n# astrology_text\u306e\u884c\u6570\u3092\u8a08\u7b97\nnum_lines = len(astrology_text)\n\n# \u73fe\u5728\u306e\u884c\u756a\u53f7\u3092\u521d\u671f\u5316\ncurrent_line = 0\n\n# \u73fe\u5728\u306e\u884c\u306e\u6301\u7d9a\u6642\u9593\u3092\u53d6\u5f97\u3059\u308b\nnow_line_duration = float(astrology_text[current_line].strip().split(&#39;,&#39;)[0])\n\n# \u30d5\u30ec\u30fc\u30e0\u751f\u6210\ndef generate_frame(t):\n    global current_line,now_line_duration,num_lines\n    frame = background.copy()\n\n    try:\n    # \u8868\u793a\u79d2\u6570\u3092\u8d85\u3048\u305f\u3089\u6b21\u306e\u30e9\u30a4\u30f3\u306b\u3059\u308b\n        # \u30c6\u30ad\u30b9\u30c8\u3092\u5206\u5272\u3057\u3066\u884c\u306b\u3059\u308b\n        duration,image_file, text = astrology_text[current_line].strip().split(&#39;,&#39;)\n        duration = float(duration)\n        draw = ImageDraw.Draw(frame)\n\n        # \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3059\u308b\u304b\u78ba\u8a8d\n        if image_file:\n            # \u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3093\u3067\u63cf\u753b\u3059\u308b\n            image = Image.open(image_file)\n            # \u753b\u50cf\u306e\u30b5\u30a4\u30ba\u3092\u53d6\u5f97\n            image_width, image_height = image.size\n\n            # \u753b\u9762\u306e\u4e2d\u5fc3\u306b\u63cf\u753b\u3059\u308b\u305f\u3081\u306e\u4f4d\u7f6e\u3092\u8a08\u7b97\n            x = (background.width - image_width) \/\/ 2\n            y = (background.height - image_height) \/\/ 2\n\n            frame.paste(image, (x, y))\n\n        if text:        \n            # \u30c6\u30ad\u30b9\u30c8\u3092\u63cf\u753b\u3059\u308b\n            font_path =&#39;\/usr\/share\/fonts\/truetype\/fonts-japanese-gothic.ttf&#39;\n            font = ImageFont.truetype(font_path, 100)\n            lines = split_text_to_lines(text, font, draw, frame.width)\n            line_height = draw.textsize(&#39; &#39;, font)[1]\n            y = background.height - len(lines) * line_height\n            for line in lines:\n                size = draw.textsize(line, font)\n                x = (background.width - size[0]) \/\/ 2\n                draw.text((x, y), line, font=font, fill=(255, 255, 255), stroke_width=5, stroke_fill=(0, 0, 0))\n                y += line_height\n\n        # \u8868\u793a\u79d2\u6570\u3092\u8d85\u3048\u3066\u3044\u305f\u3089\u6b21\u306e\u884c\u306b\u3059\u308b\n        if(int(t \/ now_line_duration) &gt; 0):\n            current_line +=1\n            duration = float(astrology_text[current_line].strip().split(&#39;,&#39;)[0])\n            now_line_duration += duration\n\n        return np.array(frame)\n    except IndexError:\n        return np.array(frame)\n\n# \u30c6\u30ad\u30b9\u30c8\u6298\u308a\u8fd4\u3057\u51e6\u7406\ndef split_text_to_lines(text, font, draw, max_width):\n    characters = list(text)\n    lines = []\n    line = &#39;&#39;\n    for character in characters:\n        new_line = line + character\n        size = draw.textsize(new_line, font)\n\n        if size[0] &gt; max_width:\n            lines.append(line.strip())\n            line = character\n        else:\n            line = new_line\n    lines.append(line.strip())\n    return lines\n\n# main\u95a2\u6570\ndef main():\n    &quot;&quot;&quot;\n    \u30e1\u30a4\u30f3\u95a2\u6570\n    &quot;&quot;&quot;\n    # \u97f3\u58f0\u30d5\u30a1\u30a4\u30eb\u3092\u751f\u6210\u30fb\u8ffd\u52a0\n    audio_clips = []\n\n    for i in range(num_lines):\n        line_duration, _, text = astrology_text[i].strip().split(&#39;,&#39;)\n        line_duration = float(line_duration)\n        speaker = 1\n        speech_clip = AudioFileClip(generate_speech(text, speaker, i), fps=44100)\n        audio_clips.append(speech_clip)\n    \n    # BGM\u3092\u8ffd\u52a0\n    music = AudioFileClip(&quot;bgm.mp3&quot;)\n    music = music.set_duration(59).set_start(0)\n\n    # \u30d3\u30c7\u30aa\u30af\u30ea\u30c3\u30d7\u3092\u751f\u6210\n    clip = VideoClip(generate_frame, duration=59)\n    audio = concatenate_audioclips(audio_clips)\n    final_audio = CompositeAudioClip([music,audio])\n    final_clip = clip.set_audio(final_audio)\n\n    # \u66f8\u304d\u51fa\u3057\n    final_clip.write_videofile(&quot;astrology.mp4&quot;,codec= &#39;mpeg4&#39;,audio_codec=&#39;aac&#39;, fps=30)\n\n# main\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\nif __name__==&quot;__main__&quot;:\n    # \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u30dd\u30a4\u30f3\u30c8\n    main()<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u3000<br>VOICEVOX\u306eAPI\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"voicevox.py\" data-lang=\"Python\"><code>import requests, tempfile, json\n\ndef generate_speech(text, speaker,i):\n# text : \u5408\u6210\u3059\u308b\u30c6\u30ad\u30b9\u30c8\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\n# speaker : \u5408\u6210\u3059\u308b\u30b9\u30d4\u30fc\u30ab\u30fc\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\n# i : \u97f3\u58f0\u30d5\u30a1\u30a4\u30eb\u756a\u53f7\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\n\n    # VOICEVOX API\u306e\u30db\u30b9\u30c8\u30a2\u30c9\u30ec\u30b9\n    host = &quot;127.0.0.1&quot; \n    # VOICEVOX API\u306e\u30dd\u30fc\u30c8\u756a\u53f7\n    port = 50021\n\n    # \u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u8a2d\u5b9a\n    params = (\n        (&quot;text&quot;, text),# \u5408\u6210\u3059\u308b\u30c6\u30ad\u30b9\u30c8\n        (&quot;speaker&quot;, speaker)# \u5408\u6210\u3059\u308b\u30b9\u30d4\u30fc\u30ab\u30fc\n    )\n\n    # audio_query\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306bPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\n    response1 = requests.post(\n        f&quot;http:\/\/{host}:{port}\/audio_query&quot;,\n        params=params\n    )\n\n    # synthesis\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306bPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\n    response2 = requests.post(\n        f&quot;http:\/\/{host}:{port}\/synthesis&quot;,\n        headers={&quot;Content-Type&quot;: &quot;application\/json&quot;},\n        params=params,\n        data=json.dumps(response1.json())\n    )\n\n    # \u5408\u6210\u3057\u305f\u97f3\u58f0\u3092\u4fdd\u5b58\n    with tempfile.TemporaryDirectory() as tmp:\n        with open(f&quot;tmp\/audi_{i}.wav&quot;, &quot;wb&quot;) as f:\n            f.write(response2.content)\n            return f&quot;tmp\/audi_{i}.wav&quot;\n<\/code><\/pre><\/div>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u3000\u4eca\u56de\u306f\u3001\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u30b7\u30ca\u30ea\u30aa\u306b\u57fa\u3065\u3044\u3066Youtube\u30b7\u30e7\u30fc\u30c8\u52d5\u753b\u3092\u4f5c\u6210\u3059\u308bPython\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u3002<br>\u3000\u4ee5\u4e0b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u52d5\u753b\u306e\u4f5c\u6210\u304b\u3089\u6295\u7a3f\u307e\u3067\u81ea\u52d5\u5316\u304c\u53ef\u80fd\u3068\u306a\u308b\u3002<br>\uff11\uff09\u52d5\u753b\u306e\u30b7\u30ca\u30ea\u30aa\u3092\u4f5c\u6210\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3002<br>\uff12\uff09<a href=\"https:\/\/developers.google.com\/youtube\/v3\/guides\/uploading_a_video?hl=ja\" title=\"Youtube\u306eAPI\">Youtube\u306eAPI<\/a>\u3092\u5229\u7528\u3057\u3066\u81ea\u52d5\u7684\u306b\u6295\u7a3f\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3002<\/p>\n\n\n\n<p>\u3000\u4eca\u56de\u306e\u3088\u3046\u306a\u661f\u5360\u3044\u306e\u30b7\u30ca\u30ea\u30aa\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u914d\u5217\u306a\u3069\u3067\u5b9f\u88c5\u3067\u304d\u308b\u306e\u3067\u3001\u4e00\u5ea6\u4ed5\u7d44\u307f\u3092\u4f5c\u308c\u3070\u52d5\u753b\u306e\u4f5c\u6210\u3054\u3068\u306bOpenAI\u306eAPI\u3092\u4f7f\u3046\u5fc5\u8981\u306f\u7121\u3044\u3002\u65e5\u3005\u306e\u30cb\u30e5\u30fc\u30b9\u306e\u307e\u3068\u3081\u52d5\u753b\u306e\u3088\u3046\u306a\u3082\u306e\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u3001<a href=\"https:\/\/ubun2m.com\/?p=1671\" title=\"WEB\u30b5\u30a4\u30c8\u306e\u60c5\u5831\u3092\u53ce\u96c6\u3057\u3001OpenAI\u3067\u8981\u7d04\u3057\u3066WP\u306b\u81ea\u52d5\u6295\u7a3f\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3010Python\u3011\">\u5225\u306e\u8a18\u4e8b<\/a>\u3067\u7d39\u4ecb\u3057\u3066\u3044\u308b\u3088\u3046\u306a\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3084OpenAI\u306b\u3088\u308b\u8981\u7d04\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u304c\u5fc5\u8981\u306b\u306a\u308b\u3060\u308d\u3046\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"affiliate-box\"><div class=\"affiliate-containar\"><a href=\"https:\/\/amzn.to\/3YNzJa9\" rel=\"nofollow\"><img decoding=\"async\" style=\"border: none;\" src=\"https:\/\/m.media-amazon.com\/images\/I\/41xznwKXTRL.jpg\" target=\"_blank\"><\/a><div class=\"affiliate-content\"><a href=\"https:\/\/amzn.to\/3YNzJa9\" rel=\"nofollow\">ChatGPT\u306e\u5165\u9580\u66f8\uff08Kindle\u7248\uff09<\/a><ul class=\"affiliate-button\"><li><a href=\"https:\/\/amzn.to\/3YNzJa9\" rel=\"nofollow\">Amazon<\/a><\/li><\/ul><\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u4ee5\u524d\u306e\u8a18\u4e8b\u3067\u3001ChatGPT\u3092\u5229\u7528\u3057\u3066\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u306e\u305f\u3081\u306e\u661f\u5ea7\u5360\u3044\u3092\u4f5c\u6210\u3057\u305f\u3002\u4eca\u56de\u306f\u3001\u661f\u5360\u3044\u306eYoutube\u30b7\u30e7\u30fc\u30c8\u52d5\u753b\u3092\u751f\u6210\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210\u3057\u305f\u306e\u3067\u7d39\u4ecb\u3059\u308b\u3002 \u30d7\u30ed\u30b0\u30e9\u30e0\u6982\u8981 \u3000\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001Linux\uff08Ubu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2918,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/ubun2m.com\/wp-content\/uploads\/2023\/02\/Attachment-1.jpg","_links":{"self":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/posts\/2908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2908"}],"version-history":[{"count":0,"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/posts\/2908\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=\/wp\/v2\/media\/2918"}],"wp:attachment":[{"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubun2m.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}