1

I'm new to the Mac Terminal. I am attempting to provide ffmpeg a .txt file containing a list of .mp4 files from a directory to concat into a new file. For this post, I only want to focus on creating the list in the way that ffmpeg requires.

My directory has four files:
file1.mp4
file2.mp4
file3.mp4
otherclip.mov

I need to have the command line look at this directory, ignore the .mov file and create a txt file in the following way.

file 'file1.mp4'
file 'file2.mp4'
file 'file3.mp4'

Assuming I'm already in the correct directory how do I loop through the files and place the .mp4 files in a txt file with the text file 'filename.mp4' wrapped around it?

I saw someone on Windows successfully do this by running for /f "tokens=*" %i in ('dir *.mp4 /b') do echo file '%~ni%~xi' >> list.txt
I'm not sure what the bash equivalent would be but when I ran that in my terminal it returned -bash: syntax error near unexpected token `"tokens=*"'

1
  • Please list the actual directory showing actual names. From the comments below it's obvious that your files are not called file1.mp4 etc.
    – nohillside
    Oct 14 '21 at 7:39
2

Assuming that you are in the the directory these files exist-

file1.mp4 file2.mp4 file3.mp4 otherclip.mov

and the output that you want into a text file is named list.txt, you could redirect the output of the following into the named file-

printf "file '%s'\n" *.mp4 > list.txt
1

Something like

cd directory/with/videofiles
for f in *.mp4; do
    echo "file '$f'" >> ./file-for-ffmpeg
done

should do.

Or

cd directory/with/videofiles
ls *.mp4 | sed -E "s|(.*)|file '\1'|" > ./file-for-ffmeg

if you look for a simple one-liner.

7
  • I am in the proper directory when running these commands. When running the first block line by line the last line starting with mv throws the operation not permitted command. That said, I do get a file-for-ffmpeg at the Finder window. When I manually add .txt to the end of it I can open and see the document contains the text file 'file*mp4' Oct 13 '21 at 16:18
  • Are you sure there are any files matching the pattern in the directory? Ah, and use cp instead of `mv‘ then.
    – nohillside
    Oct 13 '21 at 17:00
  • Using cp instead of mv prevents the error from being thrown but the txt file still just reads file 'file*mp4' Oct 13 '21 at 18:09
  • When I run cd /directory/with/videofiles rm -f /tmp/t for f in file *.MP4; do echo "file '$f'" >> /tmp/t done cp /tmp/t ./file-for-ffmpeg.txt I do get a .txt file with my list of files but the first line of the txt file reads file 'file' How would I get rid of that? Oct 13 '21 at 18:17
  • You are running the wrong command. file*mp4 must not contain a space character between file and *.mp4
    – nohillside
    Oct 13 '21 at 19:04
-1

The following worked for me: for f in *.MP4; do echo "file '$f'" >> list.txt; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .