Làm cách nào để sử dụng FFmpeg trong quy trình con Python?

Tạo một thể hiện của lớp

from better_ffmpeg_progress import FfmpegProcess
# Pass a list of FFmpeg arguments, like you would if using subprocess.run()
process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
# Use the run method to run the FFmpeg command.
process.run()
0 và cung cấp một danh sách các đối số giống như bạn sẽ làm với
from better_ffmpeg_progress import FfmpegProcess
# Pass a list of FFmpeg arguments, like you would if using subprocess.run()
process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
# Use the run method to run the FFmpeg command.
process.run()
1

Ví dụ đơn giản

from better_ffmpeg_progress import FfmpegProcess
# Pass a list of FFmpeg arguments, like you would if using subprocess.run()
process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
# Use the run method to run the FFmpeg command.
process.run()

Ví dụ nâng cao

from better_ffmpeg_progress import FfmpegProcess

def handle_progress_info(percentage, speed, eta, estimated_filesize):
    print(f"Estimated Output Filesize: {estimated_filesize / 1_000_000} MB")

def handle_success():
  # Code to run if the FFmpeg process completes successfully.
  pass

def handle_error():
  # Code to run if the FFmpeg process encounters an error.
  pass

# Pass a list of FFmpeg arguments, like you would if using subprocess.run()
process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])

ffmpeg_output_path = 'ffmpeg_output.txt'

# Use the run method to run the FFmpeg command.
process.run(progress_handler=handle_progress_info, ffmpeg_output_file=ffmpeg_output_path, success_handler=handle_success, error_handler=handle_error)

Phương thức

from better_ffmpeg_progress import FfmpegProcess
# Pass a list of FFmpeg arguments, like you would if using subprocess.run()
process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
# Use the run method to run the FFmpeg command.
process.run()
2 nhận các đối số tùy chọn sau

  • from better_ffmpeg_progress import FfmpegProcess
    # Pass a list of FFmpeg arguments, like you would if using subprocess.run()
    process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
    # Use the run method to run the FFmpeg command.
    process.run()
    
    3

    • Bạn có thể tạo một hàm nếu bạn muốn làm điều gì đó với các giá trị sau

      • Phần trăm tiến độ. [trôi nổi]
      • tốc độ, e. g.
        from better_ffmpeg_progress import FfmpegProcess
        # Pass a list of FFmpeg arguments, like you would if using subprocess.run()
        process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
        # Use the run method to run the FFmpeg command.
        process.run()
        
        4 có nghĩa là 22. 3 giây đầu vào được xử lý mỗi giây. [chuỗi]
      • ETA trong vài giây. [trôi nổi]
      • Kích thước tệp đầu ra ước tính tính bằng byte. [trôi nổi]
        • Ghi chú. Điều này là không chính xác. Vui lòng lấy giá trị bằng một hạt muối

      Hàm sẽ nhận các số liệu nói trên làm đối số, khoảng hai lần mỗi giây

  • from better_ffmpeg_progress import FfmpegProcess
    # Pass a list of FFmpeg arguments, like you would if using subprocess.run()
    process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
    # Use the run method to run the FFmpeg command.
    process.run()
    
    5 - Một đường dẫn chuỗi để xác định nơi bạn muốn lưu đầu ra của FFmpeg. Theo mặc định, tệp này được lưu trong thư mục có tên "ffmpeg_output", với tên tệp là
    from better_ffmpeg_progress import FfmpegProcess
    # Pass a list of FFmpeg arguments, like you would if using subprocess.run()
    process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "output.mp3"])
    # Use the run method to run the FFmpeg command.
    process.run()
    
    6

    Tôi đang sử dụng ffprobe từ ffmpeg để nhận thông tin về kích thước hình ảnh do người dùng chọn. Tôi đang thực hiện việc này bên trong một quy trình con, nhưng sau khi ứng dụng đã triển khai của tôi không hoạt động, tôi nhận ra rằng mình cần phát hiện đúng tệp thực thi Python để sử dụng. Tôi thấy bài viết này hữu ích để hiểu rõ hơn vấn đề là gì, nhưng tôi thực sự gặp sự cố khi giải quyết vấn đề đó. Tôi không thực sự hiểu rõ về Python cho lắm

    Đây là mã trông như thế nào bây giờ (cái này hoạt động cục bộ)

    selected_image_height = subprocess.run(f'ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=s=x:p=0 {selected_image}', stdout=subprocess.PIPE)
    

    Đây là một ví dụ về điều tôi đã thử dựa trên bài viết trợ giúp nhưng không hiệu quả

    selected_image_height = subprocess.run([f'{sys.executable}','ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 {selected_image}'], stdout=subprocess.PIPE)
    

    Tôi đã thử rất nhiều thứ khác, nhưng tôi cảm thấy như mình đang thiếu thứ gì đó thực sự cơ bản vì tôi không hiểu rõ về Python. Tôi cũng nhận ra đây có lẽ là mã thực sự xấu, xin lỗi.

    Làm cách nào để sử dụng FFmpeg trong quy trình con Python?

    • Mã số. GitHub - mrisdal/canto-podcast-creator. Một ứng dụng Streamlit mà tôi sử dụng để tạo video cho Instagram học tiếng Quảng Đông của mình
    • Ứng dụng đã triển khai. https. //mrisdal-canto-podcast-creator-create-podcast-oselu7. chiếu sáng. ứng dụng/

    Một số thông tin bổ sung trong trường hợp nó hữu ích. Tôi đang sử dụng mô-đun bộ chọn hình ảnh tùy chỉnh này để cho phép người dùng chọn từ một tập hợp các hình ảnh nằm cùng nhau trong một thư mục

    Làm cách nào để chạy FFmpeg bằng Python?

    Chạy lệnh FFmpeg từ Tập lệnh Python .
    nhập sub processffmpeg = "/ usr/local/bin/ffmpeg".
    xác định lấyUserInput(). def filterInput(tin nhắn, mặc định). .
    def buildFFmpegCommand(). final_user_input = grabUserInput() commands_list = [.
    def runFFmpeg(lệnh). nếu quy trình con. chạy (lệnh). mã trả về == 0

    FFmpeg có phải là thư viện Python không?

    Ghi chú. ffmpeg-python không cố gắng tải xuống/cài đặt FFmpeg, vì ffmpeg-python chỉ đơn thuần là một trình bao bọc Python thuần túy - trong khi cài đặt FFmpeg phụ thuộc vào nền tảng/ .

    Làm cách nào để cài đặt FFMPy trong Python?

    Cài đặt. Bạn đoán nó. cài đặt pip ffmpy
    ví dụ nhanh. >>> nhập ffmpy >>> ff = ffmpy. FFmpeg(. đầu vào = {'đầu vào. mp4'. Không có},. đầu ra = {'đầu ra. avi'. Không có}. ) >>> ff. .
    Tài liệu. http. //ffmpy. rtfd. io. Xem phần Ví dụ để biết các ví dụ sử dụng
    Giấy phép. ffmpy được cấp phép theo các điều khoản của giấy phép MIT

    Sự khác biệt giữa cuộc gọi quy trình con và chạy là gì?

    Tôi có thể nói rằng bạn sử dụng quy trình con. call() khi bạn muốn chương trình chờ quá trình hoàn tất trước khi chuyển sang quá trình tiếp theo. Trong trường hợp quy trình con. run() , chương trình sẽ cố gắng chạy tất cả các quy trình cùng một lúc, chắc chắn sẽ khiến chương trình bị sập