VideoHelp Forum
+ Reply to Thread
Results 1 to 13 of 13
Thread
  1. Hallo together,

    I'M searching for code that can filter my files to dts.

    Actual I take this code to convert dts to ac3.

    Code:
    for /R %%a in ("\*.mkv") do (ffmpeg -y -i "%%\~a" -map 0 -vcodec copy -scodec copy -acodec ac3 -b:a 640k "%%\~dpna\_AC3.mkv" )
    The problem is I have files with .mkv without dts-codec. How can I filter it?

    Thanks for your help.

    Greetings
    Quote Quote  
  2. -acodec ac3 tells ffmpeg to convert the audio to AC3. It shouldn't matter what type of audio the MKV contains. Or do I not understand what you're asking?
    Quote Quote  
  3. Tahnks for your answer.

    I just want to convert dts-files and keep the other codecs as they are.
    Quote Quote  
  4. Member
    Join Date
    Dec 2005
    Location
    Canada
    Search Comp PM
    This thread shows how to have ffmpeg use a text file list to process a bunch of files.

    https://forum.videohelp.com/threads/328303-software-that-indexes-a-drive-to-create-an-...tch-conversion

    This link below downloads 'Playtime' which can open a folder of files and list the audio codecs used.
    The codec column can be sorted to group same codecs together.
    The group of files can be hi-lighted and saved as a text file which ffmpeg may be able to use.
    I haven't tried this and I would defer to jagabo who modified the original bat file in the thread

    https://www.free-codecs.com/playtime_download.htm
    Quote Quote  
  5. Try something like:
    Code:
    for /R %%a in (*.mkv) do ffmpeg -y -i "%%~a" 2>&1 | find "Audio: dts" && ffmpeg -i "%%~a" -map 0 -c:v copy -c:a ac3 -b:a 640k -c:s copy "%%\~dpna\_AC3.mkv"
    (This ignores files without dts.)
    Quote Quote  
  6. Thank you very much for your answer.
    That is very good, thank you very much.

    Now I wanted to go one step further and rename all video files that have a dts codec to "*_dts" beforehand.
    In the next step, I would then convert them and remove the "_dts" at the end of the new file. This way I could then search for all files with _dts and remove them afterwards.

    Perhaps you can help me further here?
    Quote Quote  
  7. Code:
    for /R %%a in (*.mkv) do ffmpeg -i "%%~a" 2>&1 | find "Audio: dts" && ren "%%~a" "%%~na_dts.mkv" && ffmpeg -y -i "%%~dpna_dts.mkv" -map 0 -c:v copy -c:a ac3 -b:a 640k -c:s copy "%%~dpna_AC3.mkv"
    Last edited by sneaker; 23rd Dec 2020 at 03:55. Reason: fixed syntax error + minor improvements
    Quote Quote  
  8. Thank you very much for your answer.

    Everything that does not have DTS is already skipped correctly.

    However, I am unfortunately still getting a syntax error. Is it possible that this is related to the renaming of the file?
    Quote Quote  
  9. /edit:
    I think I have it fixed. Try again. (ren does not accept absolute path as 2nd parameter, only a filename)
    Last edited by sneaker; 23rd Dec 2020 at 03:54.
    Quote Quote  
  10. This is the last error message.
    Image
    [Attachment 56385 - Click to enlarge]


    I try it with this
    Code:
    for /R %%a in (*.mkv) do ffmpeg -y -i "%%~a" 2>&1 | find "Audio: dts" && ren "%%~dpna" "%%~na_dts.mkv" && ffmpeg -i "%%~a_dts.mkv" -map 0 -c:v copy -c:a ac3 -b:a 640k -c:s copy "%%\~dpna\_AC3.mkv"
    And then I get: The system cannot find the specified file.
    Quote Quote  
  11. My edit and your post overlapped. Try again with my updated version.
    Quote Quote  
  12. Thats the perfect code. Thanks for all.
    Quote Quote  
  13. Originally Posted by Chris9090 View Post
    Thank you very much for your answer.
    That is very good, thank you very much.

    Now I wanted to go one step further and rename all video files that have a dts codec to "*_dts" beforehand.
    In the next step, I would then convert them and remove the "_dts" at the end of the new file. This way I could then search for all files with _dts and remove them afterwards.

    Perhaps you can help me further here?

    Although not exactly what you're asking for, this will output to stdout a list of files along with their audio codecs, without writing or renaming anything. This is in sh/bash for any non-Windows users.

    ffprobe-acodec.sh
    Code:
    find . -depth -maxdepth 2 -regextype posix-extended -regex '.*\.mkv$|.*\.mp4$' -type f -exec bash -c 'echo "{}" $(ffprobe -loglevel error -select_streams a:0 -show_entries stream=codec_name -of default=nw=1:nk=1 "{}")' \;
    I use a filesystem watcher to look for media files that contain anything other than the AAC audio codec and transcode it accordingly, overwriting the original media file. Directory paths need to be altered to use it:

    ffmpeg-inotify-transcode-acodec.sh
    Code:
    #!/usr/bin/env bash
    inotifywait -m /mnt/pool0/p0ds0smb/temp/ffmpeg-inotify/. -e close_write -e moved_to |
      while read dir action file; do
        if [[ "$file" =~ ^.*\.(mp4|mkv)$ ]]; then
          bash -c "/mnt/pool0/p0ds0smb/visualblind/Documents/Scripts/linux/ffmpeg_tcode_compat.sh -d /mnt/pool0/p0ds0smb/temp/ffmpeg-inotify -w /mnt/pool0/p0ds0smb/temp/ffmpeg-inotify/.working | /usr/bin/logger -t 'ffmpeg-inotify-transcode-acodec.sh'";
        fi;
      done
    ffmpeg_tcode_compat.sh
    Code:
    #!/usr/bin/env bash
    #
    ## DESCRIPTION: Transcode with FFmpeg all (mkv, mp4) media files in specified directory,
    ##              overwriting the original source files.
    ##
    ## AUTHOR:  Travis Runyard
    ## Revised: 10/31/2020
    ## URL:     https://sysinfo.io/ffmpeg-batch-transcode-audio/
    
    # Exit on first non-zero exit code
    set -e
    # Set shell options
    shopt -s globstar
    shopt -u nullglob
    # Declare variables (optionally) are indexed arrays
    # Only associative arrays require declaration, but standardizing is a good thing
    declare -a FILENAME
    declare -a LOG
    
    # Modify TEMPDIR and WORKDIR to suit your needs
    TEMPDIR="/mnt/pool0/p0ds0smb/temp/ffmpeg"
    WORKDIR="$TEMPDIR/.working"
    SCRIPT_NAME=$(basename "$0")
    
    usage()
    {
      cat 1>&2 <<EOF
    Usage: "$SCRIPT_NAME" [options]
    
    -h| --help         Help shows this message.
    -d| --directory    Specify the directory to process.
    -w| --workdir      Writable working directory for FFmpeg.
                       If you don't specify this option, the
                       subdirectory named "working" will be used
                       within the --directory path or \$TEMPDIR.
    EOF
    }
    
    while [ "$1" != "" ]; do
        case $1 in
            -d | --directory )	shift
                                    TEMPDIR="${1:-$TEMPDIR}"
                                    WORKDIR="$WORKDIR"
                                    ;;
            -w | --workdir )	shift
                                    WORKDIR="${1:-$WORKDIR}"
                                    ;;
            -h | --help )           usage
                                    exit 0
                                    ;;
            * )                     usage
                                    exit 1
        esac
        shift
    done
    
    cd $TEMPDIR
    echo -e "*** DEBUG ***\n\$TEMPDIR = $TEMPDIR\n\$WORKDIR = $WORKDIR"
    [ -d "$WORKDIR" ] || mkdir "$WORKDIR"
    # Set field separator to newline
    OIFS="$IFS"
    IFS=$'\n'
    FILENAME=($(find . ! -path "*$(basename "$WORKDIR")/*" -regextype posix-extended -regex '.*\.(mkv|mp4)$' -type f -print))
    if [ -n "$FILENAME" ]; then
      for f in "${FILENAME[@]}"; do
        LOG+=("$f")
        AUDIOFORMAT=($(ffprobe -loglevel error -select_streams a:0 -show_entries stream=codec_name,channels -of default=nw=1:nk=1 "$f"))
      	if ! [ "${AUDIOFORMAT[0]}" = "aac" ]; then
    # Build ffmpeg array within the loop to populate variables
          args=(
          -nostdin
          -y
          -i "$f"
          -default_mode infer_no_subs
          -map 0:v:0
          -map 0:a
          -map 0:s:m:language:eng?
          -map 0:s:m:language:ger?
          -map 0:s:m:language:spa?
          -map 0:s:m:language:fre?
          -map 0:s:m:language:ita?
          -map 0:s:m:language:jpn?
          -map 0:s:m:language:kor?
          -map 0:s:m:language:por?
          -map 0:s:m:language:vie?
          -map 0:s:m:language:chi?
          -ac "${AUDIOFORMAT[1]}"
          -ar 48000
          -metadata:s:a:0 language=eng
          -metadata:s:a:0 title=
          -map_chapters -1
          -dn
          -bsf:v "filter_units=remove_types=6"
          -movflags faststart
          -c:v copy
          -c:a aac
          -c:s copy
          "$WORKDIR/$(basename "$f")"
          )
          echo -e "*** DEBUG: ffprobe detected '${AUDIOFORMAT[0]}' in the default audio stream with '${AUDIOFORMAT[1]}' channels\nPreparing to convert audio codec to AAC..." ; sleep 1
          echo -e "*** DEBUG: ffmpeg ${args[@]}"
          ffmpeg "${args[@]}" || break
          echo -e "*** DEBUG: Moving '$WORKDIR/$(basename "$f")' back to source directory name '$(dirname "$f")'"; sleep 1
          mv -f -v "$WORKDIR/$(basename "$f")" "$(dirname "$f")" || break
        fi
      done
      printf '*** DEBUG ***\nPROCESSED:%s\n'
      for i in "${LOG[@]}"; do echo "$i"; done
    else
      echo -e "*** DEBUG: No files to process"
    fi
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!