在shell脚本中创建“ffmpeg”循环中的叉“丢失”一些迭代 [英] Creating forks of `ffmpeg` in loop inside a shell script "loses" some iterations

查看:180
本文介绍了在shell脚本中创建“ffmpeg”循环中的叉“丢失”一些迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本,将目录作为输入,读取所有的 mp4 文件,并将它们转换为 mp3 ffmpeg

 #!/ bin / bash 

dir = $ 1

while read -rdfile
do
base = $(basename$ file)
filename = $ {base%(*}。mp3
ffmpeg -i$ file-vn -ar 44100 -ac 2 -ab 192000 -f mp3$ filename&
done< ;(找到$ dir-maxdepth 1 -iname* .mp4-print0)

等待

退出0
pre>

目前,所有这些对 ffmpeg 的调用都是分叉的,因为当我省略& ; 最后只转换第一个文件,其他文件被忽略。



但这是有问题的,因为某些文件错误消息如下:


路径/到/文件/文件名 - 带空格(info)(temp_information).mp4:没有这样的文件或目录


所有文件名通常都包含空格,也许这就是为什么某些文件失败的原因。



因此,我有以下问题: / p>


  1. 当我不分叉 ffmpeg 调用为什么只有第一个文件被执行?我期望循环等待,直到进程完成,然后继续下一次迭代。

  2. 为什么脚本无法找到某些文件(也许这是一个 ffmpeg )?

  3. 如何解决所有这些问题并使脚本正常工作?


解决方案

您不承认 stdin

  ffmpeg -i$ file-blah -blah -nostdin$ filename& 

shell脚本ffmpeg在2个作业之后停止



要回答评论:



你不应该无论如何,FFmpeg已经运行在多个线程以最大化CPU,甚至可能遭受
从$ th
通过分割。


I have a shell script that takes a directory as input, reads all mp4 files in it and convert them to a mp3 with ffmpeg:

#!/bin/bash

dir=$1

while read -rd "" file
do
  base=$(basename "$file")
  filename=${base%(*}".mp3"
  ffmpeg -i "$file" -vn -ar 44100 -ac 2 -ab 192000 -f mp3 "$filename" &
done < <(find $dir -maxdepth 1 -iname "*.mp4" -print0)

wait

exit 0

Currently all these calls to ffmpeg are forked because when I leave out the & at the end only the first file in converted and the others are ignored.

But this is problematic because somethings this fails on some files error messages like the following:

path/to/file/file name - with spaces(info)(temp_information).mp4: No such file or directory

All file names normally contain spaces, maybe that is the reason why it fails for some files.

Thus, I have the following questions:

  1. When I don't fork the ffmpeg calls why is only the first file executed? I expected that the loop waits until the process is finished and then continues with the next iteration.
  2. Why is the script unable to find some files (maybe it is a problem of ffmpeg)?
  3. How to solve all these problems and make the script to work?

解决方案

You are not acknowledging stdin

ffmpeg -i "$file" -blah -blah -nostdin "$filename" &

shell script ffmpeg stops after 2 jobs

To answer the comment:

You should not be forking anyway, FFmpeg already runs in multiple threads to maximize CPU, and you might even suffer from thrashing by forking it.

这篇关于在shell脚本中创建“ffmpeg”循环中的叉“丢失”一些迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆