使用Python获取文件夹中的mp3文件,并生成podcast的中间层
最终效果是这样:https://www.chatcyf.com/topics/21823/包含子目录的
import os
import uuid
import urllib.parse
from email.utils import formatdate
# ==============================
# 配置区域(需要修改)
# ==============================
AUDIO_DIR = r"\\ipfs\download\a" # 注意:不要以 \ 结尾
BASE_URL = "https://67373.chatcyf.com/a/" # 末尾务必加 /
OUTPUT_FILE = "items.xml"
# ==============================
# 工具函数:安全拼接 URL 路径
# ==============================
def build_url(base_url, relative_path):
"""
将本地相对路径转换为 URL 路径,并逐段进行 URL 编码
"""
parts = relative_path.replace("\\", "/").split("/")
encoded_parts =
return base_url + "/".join(encoded_parts)
# ==============================
# 生成 item 列表
# ==============================
items_output = ""
for root, dirs, files in os.walk(AUDIO_DIR):
for filename in files:
if not filename.lower().endswith(".mp3"):
continue
full_path = os.path.join(root, filename)
# 计算相对 AUDIO_DIR 的路径(包含子目录)
relative_path = os.path.relpath(full_path, AUDIO_DIR)
# 生成完整 URL(自动带子目录)
file_url = build_url(BASE_URL, relative_path)
# 文件大小
length = os.path.getsize(full_path)
# GUID
guid = str(uuid.uuid4())
# pubDate 使用文件修改时间
timestamp = os.path.getmtime(full_path)
pub_date = formatdate(timestamp, usegmt=True)
# 标题为文件名(去后缀)
title = os.path.splitext(filename)
item = f"""
<item>
<title>{title}</title>
<description><!]></description>
<enclosure url="{file_url}" length="{length}" type="audio/mpeg" />
<guid>{guid}</guid>
<pubDate>{pub_date}</pubDate>
<itunes:explicit>no</itunes:explicit>
</item>
"""
items_output += item
# ==============================
# 保存结果
# ==============================
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write(items_output)
print(f"音频 item 已生成:{OUTPUT_FILE}")
不包含子目录的代码
import os
import uuid
import urllib.parse
from datetime import datetime
from email.utils import formatdate
# ==============================
# 配置区域(需要修改)
# ==============================
AUDIO_DIR = r"\\ipfs\download\email" # 本地 mp3 文件夹
BASE_URL = "https://67373.chatcyf.com/Douyu/email/" # 在线 URL 前缀,末尾务必加 /
OUTPUT_FILE = "items.xml" # 最终生成 item 列表
# ==============================
# 生成 item 列表
# ==============================
items_output = ""
for filename in os.listdir(AUDIO_DIR):
if not filename.lower().endswith(".mp3"):
continue
full_path = os.path.join(AUDIO_DIR, filename)
# 获取真实文件大小(字节)
length = os.path.getsize(full_path)
# URL encode 处理
encoded_name = urllib.parse.quote(filename)
file_url = BASE_URL + encoded_name
# GUID 使用 UUID,绝对不会重复
guid = str(uuid.uuid4())
# 使用文件修改时间作为 pubDate
timestamp = os.path.getmtime(full_path)
pub_date = formatdate(timestamp, usegmt=True)
# 标题为文件名去掉后缀
title = os.path.splitext(filename)
item = f"""
<item>
<title>{title}</title>
<description><!]></description>
<enclosure url="{file_url}" length="{length}" type="audio/mpeg" />
<guid>{guid}</guid>
<pubDate>{pub_date}</pubDate>
<itunes:explicit>no</itunes:explicit>
</item>
"""
items_output += item
# 保存结果
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write(items_output)
print(f"音频 item 已生成:{OUTPUT_FILE}")
视频和音频都有,分别的
import os
import uuid
import urllib.parse
from email.utils import formatdate
# ==============================
# 配置区域
# ==============================
MEDIA_DIR = r"\\ipfs\download\67373.chatcyf.com"
BASE_URL = "https://67373.chatcyf.com/"
AUDIO_OUTPUT = "audio.xml"
VIDEO_OUTPUT = "video.xml"
# ==============================
# 工具函数:生成 URL
# ==============================
def build_url(base_url, relative_path):
parts = relative_path.replace("\\", "/").split("/")
encoded_parts =
return base_url + "/".join(encoded_parts)
# ==============================
# item 容器
# ==============================
audio_items = []
video_items = []
# ==============================
# 扫描目录
# ==============================
for root, dirs, files in os.walk(MEDIA_DIR):
for filename in files:
ext = os.path.splitext(filename).lower()
if ext == ".mp3":
mime_type = "audio/mpeg"
target_list = audio_items
elif ext == ".mp4":
mime_type = "video/mp4"
target_list = video_items
else:
continue
full_path = os.path.join(root, filename)
relative_path = os.path.relpath(full_path, MEDIA_DIR)
file_url = build_url(BASE_URL, relative_path)
length = os.path.getsize(full_path)
guid = str(uuid.uuid4())
pub_date = formatdate(os.path.getmtime(full_path), usegmt=True)
title = os.path.splitext(filename)
item = f"""
<item>
<title>{title}</title>
<description><!]></description>
<enclosure url="{file_url}" length="{length}" type="{mime_type}" />
<guid>{guid}</guid>
<pubDate>{pub_date}</pubDate>
<itunes:explicit>no</itunes:explicit>
</item>
"""
target_list.append(item)
# ==============================
# 写入文件
# ==============================
with open(AUDIO_OUTPUT, "w", encoding="utf-8") as f:
f.write("".join(audio_items))
with open(VIDEO_OUTPUT, "w", encoding="utf-8") as f:
f.write("".join(video_items))
print(f"音频 RSS item 生成完成:{AUDIO_OUTPUT}({len(audio_items)} 条)")
print(f"视频 RSS item 生成完成:{VIDEO_OUTPUT}({len(video_items)} 条)")
页:
[1]