最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to convert multiple video files in a specific path outputvideo with the same video name - Stack Overflow

programmeradmin0浏览0评论

The following code to converts a one video from mp4 to avi using ffmpeg

ffmpeg.exe -i "kingman.mp4" -c copy "kingman.avi"

I need to convert multiple videos in a specific path. "outputvideo" with the same video name

This is the my code that needs to be modified.

from pathlib import Path
import subprocess
from glob import glob

all_files = glob('./video/*.mp4')

for filename in all_files:
     videofile = Path(filename)
     outputfile = Path(r"./outputvideo/")
     codec = "copy"
     ffmpeg_path = (r"ffmpeg.exe")

outputfile = outputfile / f"{videofile.stem}"

outputfile.mkdir(parents=True, exist_ok=True)

command = [
    f"{ffmpeg_path}",
    "-i", f"{videofile}",
    "-c", f"{codec}",
    "{outputfile}",]

process = subprocess.Popen(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    text=True,
    bufsize=1,
    universal_newlines=True)
processmunicate()

The following code to converts a one video from mp4 to avi using ffmpeg

ffmpeg.exe -i "kingman.mp4" -c copy "kingman.avi"

I need to convert multiple videos in a specific path. "outputvideo" with the same video name

This is the my code that needs to be modified.

from pathlib import Path
import subprocess
from glob import glob

all_files = glob('./video/*.mp4')

for filename in all_files:
     videofile = Path(filename)
     outputfile = Path(r"./outputvideo/")
     codec = "copy"
     ffmpeg_path = (r"ffmpeg.exe")

outputfile = outputfile / f"{videofile.stem}"

outputfile.mkdir(parents=True, exist_ok=True)

command = [
    f"{ffmpeg_path}",
    "-i", f"{videofile}",
    "-c", f"{codec}",
    "{outputfile}",]

process = subprocess.Popen(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    text=True,
    bufsize=1,
    universal_newlines=True)
processmunicate()
Share Improve this question asked Jan 30 at 22:25 Oussama GamerOussama Gamer 513 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Use ffmpeg.exe if you are using windows, else just ffmpeg will work in case of linux

from pathlib import Path
import subprocess
import os
from glob import glob

input_folder = "video"
output_folder = "outputvideo"
abs_path = os.getcwd()
all_files = glob(f'{abs_path}/{input_folder}/*.mp4')
output_dir = os.path.join(abs_path , output_folder)
out_ext = "avi"

os.makedirs(output_dir , exist_ok=True)

input_dir = os.path.join(abs_path, input_folder)

for filepath in all_files:
    stem = os.path.splitext(os.path.basename(filepath))[0]
    videofile = Path(filepath)
    codec = "copy"
    ffmpeg_path = (r"ffmpeg")
    outputfile = f"{output_dir}/{stem}.{out_ext}"

    command = [
        f"{ffmpeg_path}",
        "-i", f"{videofile}",
        "-c", f"{codec}",
        f"{outputfile}",]

    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1,
        universal_newlines=True)
    stdout, stderr = processmunicate()

    # Print output for debugging
    print(f"Processed: {videofile} -> {outputfile}")
    if stderr:
        print(f"FFmpeg Error: {stderr}")

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论