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. Commented Oct 14, 2021 at 7:39

3 Answers 3

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.

5
  • 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' Commented Oct 13, 2021 at 16:18
  • Using cp instead of mv prevents the error from being thrown but the txt file still just reads file 'file*mp4' Commented Oct 13, 2021 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? Commented Oct 13, 2021 at 18:17
  • 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 get a .txt file whose only content reads file 'file*.MP4' Commented Oct 13, 2021 at 19:35
  • If the files have lowercase extensions (i.e. .mp4, not .MP4), then you need to use lowercase in the pattern: for f in file*.mp4. If there's a mix of upper- and lower-case extensions, then use for f in file*.[mM][pP]4 to match both. Commented Oct 15, 2021 at 9:17
-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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.