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 / 问题 / 1403736
Accepted
user1579004
user1579004
Asked: 2022-04-23 04:04:07 +0800 CST2022-04-23 04:04:07 +0800 CST 2022-04-23 04:04:07 +0800 CST

递归处理视频文件以腾出空间

  • 772

我已经用尽了我的存储空间,我想腾出一些空间。

我曾想过将所有文件转换为 mkv 并将高分辨率文件缩小为较低的分辨率。

所以要将 mp4 转换为 mkv 有

ffmpeg -i input.mp4 -vcodec copy -acodec copy output.mkv

为了缩小规模,有

ffmpeg -i input.mp4 -vf scale=$w:$h <encoding-parameters> output.mp4

但我需要转换所有文件而不是单个 mp4 文件,我需要在完成处理后删除输入文件,并且我必须只对高于给定分辨率的文件执行此操作。另外我想保持纵横比,我不知道这是否是这样做的。

有一个名为video-diet的工具可以完成其中的一部分。

这就是我想做的

Recursively find every file under the current directory
If the file resolution has a height equal or greater to a given height.
  Convert the file to mkv if it's in another format avi, mp4, flv, etc
  Downscale to a lower resolution height, keeping the aspect ratio.

也许我也应该将帧速率降低到 24 fps?

如果有更好的方法,我想听听。

video
  • 1 1 个回答
  • 31 Views

1 个回答

  • Voted
  1. Best Answer
    frabjous
    2022-04-23T10:50:56+08:002022-04-23T10:50:56+08:00

    这是一个让您入门的脚本。您可能需要修改它以满足您的需要,如果您想更改帧速率,请向 ffmpeg 添加选项等。

    请注意,那里有一个条件检查文件是否已经是 .mkv,但我实际上并没有对它们做不同的事情,所以条件是毫无意义的。它就在那里,因为您的问题听起来好像您想对他们做一些不同的事情,尽管我不确定为什么。FFMPEG 可以同时缩放和转换。

    但是,如果您确实想做一些不同的事情,请在条件为真时将 ffmpeg 行更改为不同的事情。

    #!/bin/bash
    
    # set the resolution above which you want to downscale
    maxwidth=1920
    maxheight=1080
    
    # set the resolution to downscale to; the width will be
    # calculated to preserve aspect ratio
    targetwidth=1280
    
    # a counter for errors
    errors=0
    
    # recursively find video files; add their names to
    # an array to process
    mapfile -t filenames < <(find $(pwd) -type f -iregex '.*avi$\|.*mp4$\|.*m4v$\|.*flv\|.*mkv\|.*ogv\|.*webm$\|.*mov')
    
    # loop through the array
    for filename in "${filenames[@]}" ; do
        # get the resolution with ffprobe
        res="$(ffprobe -hide_banner -v error -select_streams v:0 -show_entries stream=width,height -print_format csv "$filename")"
        # strip "stream" off "res"
        res="${res#stream,}"
        # parse into width and height
        width="${res%,*}"
        height="${res#*,}"
        # compare to maxwidth/height to see if it should be
        # processed
        if [[ "$width" -ge "$maxwidth" ]] || [[ "$height" -ge "$maxheight" ]] ; then
            # determine the output name
            outputname="${filename%.*}-downscaled.mkv"
            # check if output already exists; if so, skip it
            if [[ -e "$outputname" ]] ; then
                continue
            fi
            # calculate targetheight, divide by 2 multiply by 2
            # to ensure it's even
            targetheight="$(( ( ( (targetwidth * height) / width) / 2) * 2 ))"
            # get file extension
            ext="${filename##*.}"
            # do something different with mkvs?
            # not sure why you would though
            if [[ "$ext" =~ '[Mm]{Kk][Vv]' ]] ; then
                ffmpeg -i "$filename" -vf scale="$targetwidth:$targetheight" "$outputname"
                exitcode="$?"
            else
                ffmpeg -i "$filename" -vf scale="$targetwidth:$targetheight" "$outputname"
                exitcode="$?"
            fi
            # if conversion was a success, delete original file
            if [[ "$exitcode" == 0 ]] && [[ -e "$outputname" ]] ; then
                rm "$filename"
            else
                echo
                echo "Error processing $filename" >&2
                echo "Not deleted!"
                let errors++
                echo
            fi
        fi
    done
    
    # report number of errors if there were any
    if [[ "$errors" == 0 ]] ; then
        exit 0
    fi
    echo "There were $errors errors!" >&2
    exit 1
    

    这可能有点矫枉过正,可能有办法简化它,但这就是我要做的。

    • 0

相关问题

  • Ubuntu 和 VLC - 设置默认值和色调

  • 如何为我的 Sansa Fuze 转换视频?

  • 断断续续的视频播放[关闭]

  • 帮助让 Flash 播放器在第二个屏幕上工作?

  • Ubuntu 和交互式媒体安装

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