Home > database >  Can I use a text file's content to create a filtered list for ffmpeg?
Can I use a text file's content to create a filtered list for ffmpeg?

Time:10-21

I am attempting to make a CMD script that will

  1. Create a text file that lists file names followed by video codec (using ffprobe)
  2. Create a new text from the list so that any file with x265 codec is removed from the list (and formated as file "*filepath*"
  3. Run ffmpeg on the edited list to transcode remaining files to x265.

I have a script that does #1

for /R %%f IN (*.mkv,*.avi,*.mp4,*.m2ts,*.mts,*.rm,*.m4v) do echo "%%f" >>Probe.txt & ffprobe -v error -hide_banner -of default=noprint_wrappers=0 -print_format flat  -select_streams v:0 -show_entries stream=codec_name "%%f" >>Probe.txt & echo.  >>Probe.txt

which outputs

"filepath"
streams.stream.0.codec_name="codec"

"filepath"
streams.stream.0.codec_name="codec"

and I have a script that will do #3

for /R %%f IN (*.mkv,*.avi,*.mp4,*.m2ts,*.mts,*.rm,*.m4v) do ffmpeg -hide_banner -hwaccel_output_format qsv -i "%%f" -c:v libx265 -c:a ac3 -x265-params crf=25 "%%f.mkv"

I am not sure if #2 is even possible though.

End result of task 2 should be that in probe.txt, any line that has a streams.stream.0.codec_name value of anything besides hevc will have the line immediately above it written to a new txt file with the word file in front.

final goal is getting all three tasks to run under one batch file (each task running sequentially)

Is there any help on what I am missing to be able to unify these and get #2 to happen

CodePudding user response:

@ECHO OFF
SETLOCAL

for /R %%f IN (*.mkv,*.avi,*.mp4,*.m2ts,*.mts,*.rm,*.m4v) do (
 ffprobe -v error -hide_banner -of default=noprint_wrappers=0 -print_format flat  -select_streams v:0 -show_entries stream=codec_name "%%f"|FIND "streams.stream.0.codec_name"|FIND "x265">NUL
 IF ERRORLEVEL 1 ffmpeg -hide_banner -hwaccel_output_format qsv -i "%%f" -c:v libx265 -c:a ac3 -x265-params crf=25 "%%f.mkv"
)

GOTO :EOF

Totally untested

I'm assuming that your commands are correct. With no information in the post about how to detect the target files, responders unfamiliar with the details of ffmpeg are forced to make assumptions.

So - assuming that the ffprobe output contains the strings streams.stream.0.codec_name and x265 on the same line, then the result of the cascaded-finds will be that errorlevel will be set to 0 if both are found and non-0 otherwise.

So - only run the ffmpeg line if the codec detected is not x265.

Note that a file somename.mpg would (theoretically) generate somename.mpg.mkv so if it is desired that the double-barrelled extension is avoided, you would need to make some minor modifications

 if errorlevel 1 if /i "%%~xf"==".mkv" (ffmpgcommand1
  ) else ffmpgcommand2

where the location and presence of the parentheses with respect to the keywords if and else are critical; ffmpgcommand1 is the ffmpeg command with the destination filename being "%%~dpnf.converted.mkv" and ffmpgcommand2 is the ffmpeg command with the destination filename being "%%~dpnf.mkv"

I've no idea what ffmpeg does should the destination filename already exist.

I'll repeat

Theoretical only - totally untested

  • Related