{"id":581,"date":"2023-09-05T22:56:16","date_gmt":"2023-09-05T22:56:16","guid":{"rendered":"https:\/\/rodamoya.com\/?p=581"},"modified":"2023-09-05T23:19:30","modified_gmt":"2023-09-05T23:19:30","slug":"tiktok-creator","status":"publish","type":"post","link":"https:\/\/rodamoya.com\/index.php\/2023\/09\/05\/tiktok-creator\/","title":{"rendered":"TikTok creator"},"content":{"rendered":"\n<p>Too Boomer for TikTok? Not anymore! The next code is a Python code created through MoviePy and PyGoogle_Image libraries. The code creates a TikTok video given a theme for the video (this is going to be the only input). During the development of this program, I created different TikTok options. These options include creating the TikTok with personal pictures, using Google pictures with different templates, and the one I&#8217;m going to showcase, which involves using Google Images. In this option, the template also includes text to display according to the picture&#8217;s positionNext, we are going to explain all the steps of the code:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps:<\/h2>\n\n\n\n<ol>\n<li>Define the theme of the TikTok.<\/li>\n\n\n\n<li>The program uses the PyGoogle_Image library to download the images from Google, where the theme input is going to be the Google Images search.<\/li>\n\n\n\n<li>All the images are going to be saved in a specific path.<\/li>\n\n\n\n<li>Then the code adds the MP3 audio to the path with the images.<\/li>\n\n\n\n<li>Next, it will read all the files needed to create the TikTok and save them in a MoviePy format.<\/li>\n\n\n\n<li>Define the template of the TikTok with a dictionary, where the key will be the photo position, and the value will be the duration of each photo.<\/li>\n\n\n\n<li>The next step is to select the pictures that will fit best into the TikTok format, using a loop for all the pictures, where:\n<ul>\n<li>If the image size height is more than 550px, it will resize the image to fit a phone screen.<\/li>\n\n\n\n<li>Then, it will set the duration of the picture according to the position (using the dictionary).<\/li>\n\n\n\n<li>Add the picture with the duration to a list.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Set the audio of the TikTok.<\/li>\n\n\n\n<li>Convert the list with all the images to the TikTok and save it.&#8221;<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/rodamoya.com\/wp-content\/uploads\/2023\/09\/Recortado_tiktok_expl.mp4\"><\/video><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/rodamoya.com\/wp-content\/uploads\/2023\/09\/Nature_phone_wallpaper-1.mp4\"><\/video><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Code:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Imports\n\nfrom PIL import Image\nfrom IPython.display import Video\nfrom moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips\nimport os\nfrom pygoogle_image import image as pi\nfrom PIL import Image\nfrom moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip\nimport openai as ai\nimport shutil\n\n\n## Choose the theme\n\ntheme = \"Nature phone wallpaper\"  \nlimit = 100\n\n## Generate the pictures\n\ndef generar_fotos(theme, limit):\n    \n    pi.download(theme,  limit )\n\ngenerar_fotos(theme, limit)\n\n### Delet the 2 first bad pics \n\n# Main path\npath = r\"images_path\"\npath = path&#91;:-1] + theme.replace(\" \", \"_\")\n# Pics name\npic_1_delet = path + \"\/\" + theme + \"_1.png\"\npic_2_delet = path + \"\/\" + theme + \"_2.png\"\n# Delet pics:\nos.remove(pic_1_delet)\nos.remove(pic_2_delet)\n\n## ELiminar la carpeta con las fotosuna vez se ha creado el video\n\n### Move audio\n\nsource_file = r'path... TIKTOK\\Audios\\Icantexplain.mp3'\n\nif not os.path.exists(source_file):\n    print(\"Source file does not exist.\")\nelse:\n    # Check if the destination folder exists\n    if not os.path.exists(path):\n        print(\"Destination folder does not exist.\")\n    else:\n        # Attempt to copy the file\n        try:\n            shutil.copy(source_file, os.path.join(path))\n            print(\"File copied successfully.\")\n        except PermissionError as e:\n            print(f\"PermissionError: {e}\")\n\n# Read content\n\n#25 fotos + audio\n\ndef read_files_path(path):\n    #Contadores\n    contador_files = 0\n    contador_files_mp3 = 0\n    contador_files_vid = 0\n    contador_files_img = 0\n    \n    #dicionario output\n    dic_files = {}\n\n\n\n    # Get a list of all files in the path\n    files = os.listdir(path)\n\n    # Iterate over each file in the list\n    for file in files:\n        # Create the file's absolute path\n        file_path = os.path.join(path, file)\n\n        # Check if the file is a regular file\n        if os.path.isfile(file_path):\n\n            #Obtain the path\n            extension = file_path&#91;-4:].upper()\n\n            #mp3:\n            if extension == '.MP3':\n                \n                audio = file_path               \n                \n                \n                #Name dic\n                name_mp3 = \"mp3_file\" + str(contador_files_mp3)\n                #Save dic\n                #dic_files&#91;name_mp3] = file_path\n                #Add 1 to counter\n                contador_files_mp3 = contador_files_mp3 + 1\n\n            #Imagen:\n            if (extension == '.JPG') | (extension == '.PNG')  | (extension == 'JPEG'):\n                #Name dic\n                name_img = \"img_file\" + str(contador_files_img)\n                #Save dic\n                dic_files&#91;name_img] = file_path\n                #Add 1 to counter\n                contador_files_img = contador_files_img +1\n\n            #Video:\n            if (extension == '.MOV') | (extension == '.MP4'):\n                #Name dic\n                name_vid = \"vid_file\" + str(contador_files_vid)\n                #Save dic\n                dic_files&#91;name_vid] = file_path\n                #Add 1 to counter\n                contador_files_vid = contador_files_vid +1\n\n\n\n            # Perform operations on the file\n            print(file_path)\n            \n    return dic_files, audio\n\n#path = r\"\" # Path with images.\ndic_files, audio  = read_files_path(path)\n\naudio = AudioFileClip(audio)\n\n### RESIZE;\n\ndef resize(path):\n\n    # Open the image file\n    image = Image.open(path)\n\n    # Define the desired resolution (width, height)\n    desired_width = 480\n    desired_height = 848\n\n    # Calculate the center coordinates for cropping\n    left = (image.width - desired_width) \/\/ 2\n    upper = (image.height - desired_height) \/\/ 2\n    right = left + desired_width\n    lower = upper + desired_height\n\n    # Crop the image\n    cropped_image = image.crop((left, upper, right, lower))\n\n    # Resize the cropped image to the desired resolution\n    resized_image = cropped_image.resize((desired_width, desired_height))\n\n    # Save the resized image\n    resized_image.save(path)\n\n\n\n# Leer y clasificar files:\n\n### \n\n# Tik tok with text\n\ndicionario_Icantexplain = {\n    \"Amount\":13,\n    0:2.1,\n    1:2.4,\n    2:1.2,\n    3:1.4,\n    4:0.7,\n    5:1.4,\n    6:1,\n    7:1,\n    8:1,\n    9:0.8,\n    10:0.7,\n    11:1.1,\n    12:1,\n    13:1.2,\n    14: \"I can't explain\",\n    15: \"I got a feeling that I just\",\n    16: \"I can't erase\",\n    17: \"Just a feeling that\",\n    18: \"I won't\",\n    19: \"Won't leave behind\",\n    20: \"Because it's something\",\n    21: \"That is on\",\n    22: \"It's on my mind\",\n    23: \"I guess it goes like\",\n    24:\"na-na-na\",\n    25:\".\",\n    26:\".\",\n    27:\".\",\n    28:\".\",\n    29:\".\"\n    \n    \n}\n\ndicionario = dicionario_Icantexplain\n\nmedia_list= &#91;]\ni = 0\nfor file_name in dic_files.keys():\n    \n    # SI ya te el numero de fotos neccesarias parar\n    if i >= (dicionario&#91;\"Amount\"]+1):\n        break\n    # Print Nombre:path\n    print(dic_files&#91;file_name])\n    \n    # Get the path name\n    file_path = dic_files&#91;file_name]\n    \n    # Abrir imagen y descartar si toca\n    image = Image.open(file_path)\n    if (image.size&#91;1]>= 550): #&amp; (( (image.size&#91;0] \/ image.size&#91;1]) > 0.45 ) &amp; ( (image.size&#91;0] \/ image.size&#91;1]) &lt; 0.65 ))\n\n        try:\n            \n\n\n            #Ajustar Imagen\n            resize(file_path)               \n\n            #Read img\n            video_clip = VideoFileClip(file_path)\n            video_clip = video_clip.set_duration(dicionario&#91;i])\n\n            # Create a TextClip with the desired text\n            try:\n                print(dicionario&#91;i+14], dicionario&#91;i])\n                text_clip = TextClip(dicionario&#91;i+14], fontsize=20, color='white', bg_color =\"black\")\n            except:\n                text_clip = TextClip(\",\", fontsize=20, color='white', bg_color =\"black\")\n                print(\"good\", i )\n                \n                \n\n            # Position the text on the video (adjust x and y as needed)\n            x_position = video_clip.w \/ 2 - text_clip.w \/ 2\n            y_position = video_clip.h \/ 2 - text_clip.h \/ 2\n            text_clip = text_clip.set_position((x_position, y_position)).set_duration(dicionario&#91;i])\n\n            # Composite the TextClip on top of the video\n            composite_clip = CompositeVideoClip(&#91;video_clip, text_clip])\n\n            #Audio:\n            media = composite_clip.set_audio(audio)\n\n            # A\u00f1adir a la lista de fotos a mostrar\n            media_list.append(media)\n            i = i +1 \n                \n            \n            \n            \n        except:\n            print(\"Error\")\n            pass\n\n\n    else:\n        print(\"Imagen peque\u00f1a\")\n    \n\nname_file = theme.replace(\" \",\"_\")\nname_file = name_file + \".mp4\"\nname_file\n\nlen(media_list)\n\n# Concatenate the video clips\nfinal_video = concatenate_videoclips(media_list)\n\n# Set the final video's audio to the desired audio\nfinal_video = final_video.set_audio(audio)\n\n# Display the video in Jupyter Notebook or JupyterLab\n#Video(final_video)\n\n# Export the final video with changed audio\nfinal_video.write_videofile(name_file)  <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Too Boomer for TikTok? Not anymore! The next code is a Python code created through MoviePy and PyGoogle_Image libraries. The code creates a TikTok video given a theme for the video (this is going to be the only input). During the development of this program, I created different TikTok options. These options include creating the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/rodamoya.com\/index.php\/2023\/09\/05\/tiktok-creator\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;TikTok creator&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/581"}],"collection":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":2,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":587,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions\/587"}],"wp:attachment":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}