From 5cb6394e2725b70ed4f75beddbad6fb4514cbe17 Mon Sep 17 00:00:00 2001
From: Siarhei Siniak <siarheisiniak@gmail.com>
Date: Sat, 6 Jul 2024 17:43:37 +0300
Subject: [PATCH] [~] Refactor

---
 python/tasks/tiktok/__init__.py | 69 +++++++++++++++++++++++++++++++--
 1 file changed, 66 insertions(+), 3 deletions(-)

diff --git a/python/tasks/tiktok/__init__.py b/python/tasks/tiktok/__init__.py
index 2603852..bdc249f 100644
--- a/python/tasks/tiktok/__init__.py
+++ b/python/tasks/tiktok/__init__.py
@@ -24,6 +24,7 @@ class tiktok_config_t:
         project_root: str=''
         cache: str=''
         videos: str=''
+        audios: str=''
 
 def tiktok_config() -> tiktok_config_t.res_t:
     res = tiktok_config_t.res_t(
@@ -43,8 +44,13 @@ def tiktok_config() -> tiktok_config_t.res_t:
         res.cache,
         'videos',
     )
+    res.audios = os.path.join(
+        res.cache,
+        'audios',
+    )
 
     os.makedirs(res.videos, exist_ok=True)
+    os.makedirs(res.audios, exist_ok=True)
 
     return res
 
@@ -269,6 +275,8 @@ def tiktok_videos_process(meta: Iterable[dict[str, Any]]) -> dict[str, Any]:
         error=0,
     )
 
+    song = audio_get()
+
     for o in tqdm.tqdm(meta):
         stats['total'] += 1
 
@@ -282,13 +290,27 @@ def tiktok_videos_process(meta: Iterable[dict[str, Any]]) -> dict[str, Any]:
 
             processed_path = path_parts[0] + '-proc' + path_parts[1]
 
+            print(processed_path)
             if not os.path.exists(path) or os.path.exists(processed_path):
                 stats['skipped'] += 1
                 continue
 
-            subprocess.check_call([
-                'ffmpeg','-i', path, '-filter:v', 'setpts=0.5*PTS', processed_path,
-            ])
+            ffmpeg = [
+                'ffmpeg',
+                '-i', path,
+                '-i', song.path_mp3,
+                '-shortest',
+                '-vf',
+                ','.join([
+                    'setpts=1.1*PTS',
+                    'scale=trunc(iw/0.9):trunc(ow/a/2)*2',
+                ]),
+                '-sws_flags', 'bilinear',
+                '-map', '0:v:0',
+                '-map', '1:a:0',
+                processed_path,
+            ]
+            subprocess.check_call(ffmpeg)
             stats['saved'] += 1
         except KeyboardInterrupt:
             break
@@ -299,3 +321,44 @@ def tiktok_videos_process(meta: Iterable[dict[str, Any]]) -> dict[str, Any]:
             stats['error'] += 1
 
     return stats
+
+class audio_get_t:
+    @dataclasses.dataclass
+    class res_t:
+        file: str
+        file_mp3: str
+        path: str
+        path_mp3: str
+        url: str
+
+def audio_get() -> audio_get_t.res_t:
+    c = tiktok_config()
+    url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
+    file = 'song.dat'
+    file_mp3 = 'song.mp3'
+
+    path = os.path.join(c.audios, file)
+    path_mp3 = os.path.join(c.audios, file_mp3)
+
+    if not os.path.exists(path):
+        subprocess.check_call([
+            'yt-dlp',
+            '-f', 'bestaudio',
+            url,
+            '-o', path,
+        ])
+
+    if not os.path.exists(path_mp3):
+        subprocess.check_call([
+            'ffmpeg',
+            '-i', path,
+            path_mp3,
+        ])
+
+    return audio_get_t.res_t(
+        file=file,
+        file_mp3=file_mp3,
+        path=path,
+        path_mp3=path_mp3,
+        url=url,
+    )