videoanywhere / playwithmpv

A server to support playing videos with mpv.
MIT License
41 stars 2 forks source link

[Feature Request] Dash Support #9

Open BakaJzon opened 1 year ago

BakaJzon commented 1 year ago

mpv 是支持外挂音轨的,能否提供对 dash 播放的支持?

MacKenia commented 10 months ago

在我的测试下,想要挂载m4s为音轨的话只有通过命令行,我通过此方法支持了Dash格式,但你需要注意一下几点:

  1. 视频名称将会变成乱码
  2. 视频音轨需要自己手动选择
  3. 列表模式视频数将大幅减少(视频和音频链接太长)
  4. 保留原有flv格式

食用方法: 找到 playwithmpv 的模块存放地址,Windows一般在 %APPDATA%\Python\Python310\site-packages\playwithmpv 修改 server.py 中的 play_with_mpv 函数模块 引入 re 模块 添加 get_file_format 函数模块

# 引入 re 模块
import re

# 添加 get_file_format 函数模块
def get_file_format(url: str) -> str:
    """
    Return the file format of a URL if it is either flv, mp4, or m4a.
    """
    match = re.search(r'\.(flv|mp4|m4a|m4s)', url)
    if match:
        return match.group(1)
    else:
        return "unknown"

# 修改 play_with_mpv 函数模块
@app.route("/", methods=["GET", "POST"])
@use_args(video_args)
def paly_with_mpv(args):
    """
    1. Handle requests and parse input data.
    2. Generate mpv args and mpv command.
    3. Execute mpv command.
    """
    # Geneate video direct url
    if args["user-agent"] is None:
        args["user-agent"] = DEFAULT_USER_AGENT

    # whether dash or flv
    flv = True
    if get_file_format(args["titles"][0]) == "flv":
        print("flv video format")
        args["video_urls"] = map(create_valid_url, args["urls"])
        args["video_titles"] = args["titles"][::]
    else:
        print("dash video format")
        flv = False
        args["video_urls"] = map(create_valid_url, args["urls"][::2])
        args["audio_urls"] = map(create_valid_url, args["urls"][1::2])
        args["video_titles"] = args["titles"][::2]

    # Geneate mpv args and mpv command
    mpv_httpd_header = '--http-header-fields="referer:{0}","user-agent:{1}"'.format(
        args["referer"], args["user-agent"]
    )

    # Generate m3u playlist file
    user_dir = "USERPROFILE" if os.name == "nt" else "HOME"
    playlist_file = os.path.join(os.environ[user_dir], "_tmp_playlist.m3u")
    if flv:
        with open(playlist_file, "w", encoding="utf-8") as f:
            # Whether delete the tmp playlist file? Look a time xixi.
            f.write("#EXTM3U\n")
            f.write("#Generated by Wullic-videoanywhere modify by mac\n")
            for title, video_url in zip(args["video_titles"], args["video_urls"]):
                f.write("#EXTINF:-1," + title + "\n")
                f.write(video_url + "\n")
        mpv_cmd = " ".join([args["dir"], mpv_httpd_header, '"{0}"'.format(playlist_file)])

    else:
        video_lists = ""
        for video_url, audio_url in zip(args["video_urls"], args["audio_urls"]):
            video_lists += (f" \"{video_url}\" --audio-file=\"{audio_url}\"")
        mpv_cmd = " ".join([args["dir"], mpv_httpd_header, video_lists])

    print(mpv_cmd)
    print("\n".join(args["video_titles"]))

    # Execute mpv command
    ret = os.system(mpv_cmd)
    if ret != 0:
        return {
            "success": False,
            "message": "MPV: command not found. Ensure your mpv path",
        }
    else:
        return {"success": True, "message": ""}