乐闻世界logo
搜索文章和话题

Can ffmpeg show a progress bar?

1个答案

1

ffmpeg itself, when run in the command line, by default displays progress information in the standard output. However, this is not a traditional graphical progress bar; it shows current transcoding time, speed, frame count, and other metrics in text format rather than as a visual progress bar.

You can parse this text information from ffmpeg's output using additional scripts or programs to generate a graphical progress bar. For example, you can use Python or Shell scripts to read ffmpeg's output, analyze the progress data, and display a graphical progress bar.

Examples:

For instance, you can use Python libraries such as tqdm to achieve this. Here is a simple example code that demonstrates how to parse ffmpeg's output and display a progress bar:

python
import subprocess import sys from tqdm import tqdm def run_ffmpeg(input_file, output_file): cmd = f"ffmpeg -i {input_file} -some_options {output_file}" process = subprocess.Popen( cmd, stderr=subprocess.PIPE, universal_newlines=True ) duration = None progress = tqdm(total=100, file=sys.stdout, desc='Processing', leave=True) while True: line = process.stderr.readline() if not line: break if "Duration" in line: duration_str = line.split(",")[0].split("Duration:")[1].strip() hours, minutes, seconds = map(float, duration_str.split(":")) duration = hours * 3600 + minutes * 60 + seconds if "time=" in line: time_str = line.split("time=")[1].split(" ")[0] hours, minutes, seconds = map(float, time_str.split(":")) current_time = hours * 3600 + minutes * 60 + seconds progress.update((current_time / duration) * 100 - progress.n) progress.close() # Example usage run_ffmpeg("input.mp4", "output.mp4")

In this code, we first launch a ffmpeg subprocess and monitor its standard error output (since ffmpeg outputs progress information to stderr). We parse the total duration and current progress time from the output and use this information to update the progress bar provided by the tqdm library.

Of course, this is a basic example; you may need to adjust and optimize it based on specific requirements, such as handling time formats more precisely or adding error handling and exception handling.

2024年6月29日 12:07 回复

你的答案