AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1079725
Accepted
Andy Pittas
Andy Pittas
Asked: 2018-10-01 05:36:23 +0800 CST2018-10-01 05:36:23 +0800 CST 2018-10-01 05:36:23 +0800 CST

FFmpeg 的双文件扩展名

  • 772

我试图让 FFmpeg 遍历我运行命令的路径下的所有文件和子文件夹,以将任何 MKV 文件转换为 MP4 容器。

我已经让下面的命令工作了,但是除了新的 MP4 扩展名之外,创建的新 MP4 文件仍然具有 MKV 扩展名。

find ./ -iname '*.avi' -o -iname '*.mkv' -exec bash -c 'ffmpeg -i "{}" -vcodec copy -acodec copy "{}".mp4' \;

例如 :

abcd.mkv (original file)  
abcd.mkv.mp4 (new file)

如何调整命令以摆脱原始文件扩展名?

command-line ffmpeg find mkv mp4
  • 4 4 个回答
  • 1480 Views

4 个回答

  • Voted
  1. andrew.46
    2018-10-01T22:59:07+08:002018-10-01T22:59:07+08:00

    如果您热衷于保留现有语法(这可能对您来说很有意义),那么以下轻微修改应该保持这个含义,同时还完成所需的文件名重命名:

    find -iname '*.avi' -o -iname '*.mkv' -exec \
        bash -c 'ffmpeg -i "{}" -codec copy \
        $(echo "{}" | sed -r 's/.{3}$/mp4/')' \;
    

    请注意,我还使用 FFmpeg简化了您的copy语法...

    • 2
  2. Best Answer
    waltinator
    2018-10-01T15:19:42+08:002018-10-01T15:19:42+08:00

    有一种更简单的方法可以做到这一点,将您的ffmpeg命令放入bash脚本并使用bash工具。代替:

    find ./ -iname '*.avi' -o -iname '*.mkv' -exec \
        bash -c 'ffmpeg -i "{}" -vcodec copy -acodec copy "{}".mp4' \;
    

    使用findandxargs将文件名列表(包括带空格的文件名)传递给例如$HOME/bin/fixthem(read man find;man xargs):

    find . -type f -iname '*.avi' -o -iname '*.mkv' -print0  |\
        xargs -0 -r $HOME/bin/fixthem
    

    $HOME/bin/fixthem像(和' chmod +xd)这样的东西:

    注意:未经测试,但通过/usr/bin/shellcheck.

    #!/bin/bash
    # convert the *.mkv and *.avi files to .mp4
    
    # determine my name
    me=$0
    me=${me##*/}
    
    # -h or --help 
    help () {
        cat >&2 <<EOF
    ${me} [-h|--help] [-d|--debug] [-v|--verbose] [-n|--noaction] file file ...
    ${me}: -h or --help      This message
    ${me}: -d or --debug     List the commands we execute, as we process
    ${me}:                   the files.
    ${me}: -v or --verbose   List the filenames we process, as we process
    ${me}:                   the files.
    ${me}: -n or --noaction  Don't actually execute the commands. Used in
    ${me}:                   connection with --debug for testing.
    ${me}: -r or --remove    Remove the input file unless --debug is set.
    EOF
        exit 2
    }
    
    declare -i debug=0 verbose=0 noaction=0 remove=0
    
    # from /usr/share/doc/util-linux/examples/getopt-parse.bash 2015-Sep-06
    
    TEMP=$(getopt -o dhvnr --long debug,help,verbose,noaction,remove \
         -n 'fixthem.bash' -- "$@")
    
    if [[ $? != 0 ]] ; then echo "${me} --help for help." >&2 ; exit 1 ; fi
    
    # Note the quotes around `$TEMP': they are essential!
    eval set -- "$TEMP"
    
    while true ; do
        case "$1" in
            -d|--debug) debug=1; shift;;
            -h|--help) help; shift;;
            -v|--verbose) verbose=1; shift;;
            -n|--noaction) noaction=1; shift;;
            -r|--remove) remove=1; shift;;
            --) shift; break;;
            *) echo "Internal error! ${me} --help for help";exit 1;;
        esac
    done
    
    # actual processing begins here
    while [[ $# -gt 0 ]] ; do
          infile="$1"
          shift
          nameonly="${infile%.*}"
          outfile="${nameonly}.mp4"
          [[ "$verbose" -ne 0 ]] && echo "$infile -> $outfile" >&2
          command="ffmpeg -i \"$infile\" -vcodec copy -acodec copy \"$outfile\""
          [[ "$debug" -ne 0 ]] && echo "command" >&2
          [[ "$noaction" -ne 0 ]] || eval "$command"
          if [[ "$remove" -ne 0 ]] ; then
          v=""
          [[ "$verbose " -ne 0 ]] && v="-v"
          if [[ "$debug" -ne 0 ]] ; then
              echo "rm \"$infile\"" >&2
          else
              rm $v "$infile"
          fi
          fi
    done
    
    exit 0
    
    • 0
  3. Rajib
    2019-12-20T10:36:13+08:002019-12-20T10:36:13+08:00

    另一种方式,如果您的文件名没有空格、换行符或其他奇怪的字符:

    for file in $(find -iname '*.avi' -o -iname '*.mkv')
    
    do
    ffmpeg -i $file -codec copy ${file%%.*}.mp4
    done
    

    这将删除最后一个点 (.) 之后的所有扩展名,然后添加.mp4. 如果文件名中可能有空格,请改用:

    find . -iname '*.avi' -o -iname '*.mkv' -print0 | while read -d $'\0' file;
    
    do
      ffmpeg -nostdin -i "$file" -codec copy ${file%%.*}.mp4
    done
    
    • 0
  4. iordanis Sofiad jordan AE
    2020-06-06T00:32:08+08:002020-06-06T00:32:08+08:00

    由于弄乱很多代码有点麻烦,我只是在编码开始之前添加了一个重命名命令并删除了.mp4扩展名。

    ren Troc*.mp4 Troc*.
    for %%a in ("Troc*.") do "F:\Dropbox\PortableApps\ffmpeg" -i "%%a" -c:v libx264  -crf 22 -c:a aac -b:a 128k "%%a(ff).mp4"
    

    这是我文件夹中的屏幕截图:

    对于名称已调整的文件,缩略图显示正确

    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve