'commit'
This commit is contained in:
77
DouYin/convert_to_mp4.py
Normal file
77
DouYin/convert_to_mp4.py
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger("VideoConverter")
|
||||
|
||||
def get_codec(filepath):
|
||||
try:
|
||||
cmd = [
|
||||
"ffprobe",
|
||||
"-v", "error",
|
||||
"-show_entries", "stream=codec_name",
|
||||
"-of", "default=noprint_wrappers=1:nokey=1",
|
||||
filepath
|
||||
]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||
return result.stdout.strip().split('\n')
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking codec for {filepath}: {e}")
|
||||
return []
|
||||
|
||||
def convert_to_h264(filepath):
|
||||
try:
|
||||
directory = os.path.dirname(filepath)
|
||||
filename = os.path.basename(filepath)
|
||||
name, ext = os.path.splitext(filename)
|
||||
temp_filepath = os.path.join(directory, f"{name}_temp{ext}")
|
||||
|
||||
logger.info(f"Converting {filename} to H.264...")
|
||||
|
||||
cmd = [
|
||||
"ffmpeg",
|
||||
"-i", filepath,
|
||||
"-c:v", "libx264",
|
||||
"-c:a", "copy",
|
||||
"-y",
|
||||
temp_filepath
|
||||
]
|
||||
|
||||
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
|
||||
# Replace original file
|
||||
os.remove(filepath)
|
||||
os.rename(temp_filepath, filepath)
|
||||
logger.info(f"Successfully converted: {filename}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to convert {filepath}: {e}")
|
||||
if os.path.exists(temp_filepath):
|
||||
os.remove(temp_filepath)
|
||||
return False
|
||||
|
||||
def main():
|
||||
directory = r"d:\dsWork\aiData\DouYin\DownloadedVideos"
|
||||
if not os.path.exists(directory):
|
||||
logger.error(f"Directory not found: {directory}")
|
||||
return
|
||||
|
||||
files = [f for f in os.listdir(directory) if f.endswith(".mp4")]
|
||||
total = len(files)
|
||||
logger.info(f"Found {total} video files.")
|
||||
|
||||
for i, filename in enumerate(files, 1):
|
||||
filepath = os.path.join(directory, filename)
|
||||
codecs = get_codec(filepath)
|
||||
|
||||
if "hevc" in codecs:
|
||||
logger.info(f"[{i}/{total}] HEVC detected: {filename}")
|
||||
convert_to_h264(filepath)
|
||||
else:
|
||||
logger.info(f"[{i}/{total}] Skipping (already compatible or unknown): {filename} ({codecs})")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user