I have this script on .bat file that will process any .mp4 file and create a new one with same name but adds -no-logo after the name for each video file
The problem is that I have 150 folder each has video files that I want to run this script on, is there a way to run it on each folders one by one, and do the same task
script
for %%a in (".mp4") do ffmpeg -i "%%a" -filter:v "crop=1280:700:0:0" -c:a copy "%%~na-old.mp4"
for %%a in (".mp4") do ffmpeg -i "%%~na-old.mp4" -vf scale=1280:720,setsar=1:1 "%%~na-no-Logo.mp4"
for %%a in ("*.mp4") do del "%%~na-old.mp4"
I don't want it to run 150 time at once, this will kill my PC, I want it to go one folder at a time
.If I understood your question correctly, I would get two "for". However it is not clear if you are using linux bash or Windows bat because you selected the bash subject but you mention using .bat.
The below is going to process one MP4 at the time anyway. Do one folder, then the next until all is done.
for dir in */; do
printf "\n\nFolder processed: ${dir}\n\n"
count=0
for mp4file in $dir"**/#(*.mp4); do
ffmpeg -i "$mp4file" -y
-loglevel error \
-v quiet -stats \
place your ffmpeg command here
count+=1
done
printf "\n MP4 processed count = ${count}\n"
done
You can continue from there.
The command below is to convert from mp4 file to jpg file with the same file name in the same directory, .../httpdocs/save/.
[root#server-xxxxx-x ~]# for i in `find /var/www/vhosts/xxxxxx.com/httpdocs/save/ -type f -name "*.mp4"`; do ffmpeg -i $i `echo $i | sed -En "s/.mp4$/.jpg/p"`; done
Now, I need to convert from, .../httpdocs/save/ to the different directory, .../httpdocs/file/, how should I change the command above? I'd appreciate if anyone could help me out.
ffmpeg version 2.2.2
Simple method is to execute the command from the directory containing the input files:
cd "/path/to/inputs" && for i in *.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/${i%.*}.jpg"; done
Or you could use basename if you want to put the full path in the for loop:
for i in /path/to/inputs/*.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/$(basename "$i" .mp4).jpg"; done
...but it is less efficient than only using parameter expansion:
for i in /path/to/inputs/*.mp4; do filepath="${i##*/}"; ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/${filepath%.*}.jpg"; done
Other relevant questions:
How do you convert an entire directory with ffmpeg?
Use ffmpeg to get middle frame of a video?
How can I extract a good quality JPEG image from an H264 video file with ffmpeg?
Is there a way to combine the following two ffmpeg commands into one?
ffmpeg -i OutputAudioEN.mp4 -acodec aac -strict -2 german.mp4
ffmpeg -i german.mp4 -c copy -f segment
-segment_list audio-de.m3u8 -segment_time 10 output%03d.ts
Is this possible to use the output from the first command for the second line, without using two separate commands?
Well, here I'm making my comment as a proper answer. What I'm suggesting here is you can directly segment the video and then encode the audio using acc according to your need. Following command works for me.
ffmpeg -i OutputAudioEN.mp4 -f segment -segment_list audio-de.m3u8 -segment_time 10 -acodec aac -strict -2 output%03d.ts
Like this you can segment the video while audio encoding also happens at the meantime.
Hope this helps you!
Can anyone walk me through the steps of taking a video from our website and changing it to a m3U8 so we can then insert it to our app dashboard to have video on our app?
Download FFMPEG
then download a http segmenter e.g. apple's one or the google one
Now run the command prompt using cmd.
Go to bin directory of the FFMPEG from the command prompt.
Type ffmpeg –h to find various option of the ffmpeg.
This sample code converts a mp4 to a ts file:
ffmpeg -i myvideo.mp4 -acodec libfaac -ar 48000 -ab 64k -s 320x320 -vcodec libx264 -vbsf h264_mp4toannexb -f mpegts myvideo.ts
then run the segmenter to break up the ts file into smaller segments
segmenter -i myvideo.ts -d 20 -o la -x test/myvideo.m3u8
add the following to your htaccess file if you are running on apache
.M3U8 application/x-mpegURL
.ts video/MP2T
that's about it
Use gstreamill, which can transcode video to m3u8 format.
I have list of files which contain particular patterns, but those files have been tarred. Now I want to search for the pattern in the tar file, and to know which files contain the pattern without extracting the files.
Any idea...?
the tar command has a -O switch to extract your files to standard output. So you can pipe those output to grep/awk
tar xvf test.tar -O | awk '/pattern/{print}'
tar xvf test.tar -O | grep "pattern"
eg to return file name one pattern found
tar tf myarchive.tar | while read -r FILE
do
if tar xf test.tar $FILE -O | grep "pattern" ;then
echo "found pattern in : $FILE"
fi
done
The command zgrep should do exactly what you want, directly.
for example
zgrep "mypattern" *.gz
http://linux.about.com/library/cmd/blcmdl1_zgrep.htm
GNU tar has --to-command. With it you can have tar pipe each file from the archive into the given command. For the case where you just want the lines that match, that command can be a simple grep. To know the filenames you need to take advantage of tar setting certain variables in the command's environment; for example,
tar xaf thing.tar.xz --to-command="awk -e '/thing.to.match/ {print ENVIRON[\"TAR_FILENAME\"] \":\", \$0}'"
Because I find myself using this often, I have this:
#!/bin/sh
set -eu
if [ $# -lt 2 ]; then
echo "Usage: $(basename "$0") <pattern> <tarfile>"
exit 1
fi
if [ -t 1 ]; then
h="$(tput setf 4)"
m="$(tput setf 5)"
f="$(tput sgr0)"
else
h=""
m=""
f=""
fi
tar xaf "$2" --to-command="awk -e '/$1/{gsub(\"$1\", \"$m&$f\"); print \"$h\" ENVIRON[\"TAR_FILENAME\"] \"$f:\", \$0}'"
This can be done with tar --to-command and grep --label:
tar xaf archive.tar.gz --to-command 'egrep -Hn --label="$TAR_FILENAME" your_pattern_here || true'
--label gives grep the filename
-H tells grep to display the filename, and -n the line number
|| true because otherwise grep will exit with an error if the pattern is not found, and tar will complain about that.
xaf means to extract, and automagically decompress based off the file extension
--to-command has tar pass each file in the tarfile to a separate invocation of grep, and sets various environment variables with info about the file. See the manpage for more info.
Pretty heavily based off of Chipaca's answer (and Daniel H's comment), but this should be a bit easier to use and just uses tar and grep.
Python's tarfile module along with Tarfile.extractfile() will allow you to inspect the tarball's contents without extracting it to disk.
The easiest way is probably to use avfs. I've used this before for such tasks.
Basically, the syntax is:
avfsd ~/.avfs # Sets up a avfs virtual filesystem
rgrep pattern ~/.avfs/path/to/file.tar#/
/path/to/file.tar is the path to the actual tar file.
Pre-pending ~/.avfs/ (the mount point) and appending # lets avfs expose the tar file as a directory.
That's actually very easy with ugrep option -z:
-z, --decompress
Decompress files to search, when compressed. Archives (.cpio,
.pax, .tar, and .zip) and compressed archives (e.g. .taz, .tgz,
.tpz, .tbz, .tbz2, .tb2, .tz2, .tlz, and .txz) are searched and
matching pathnames of files in archives are output in braces. If
-g, -O, -M, or -t is specified, searches files within archives
whose name matches globs, matches file name extensions, matches
file signature magic bytes, or matches file types, respectively.
Supported compression formats: gzip (.gz), compress (.Z), zip,
bzip2 (requires suffix .bz, .bz2, .bzip2, .tbz, .tbz2, .tb2, .tz2),
lzma and xz (requires suffix .lzma, .tlz, .xz, .txz).
For example:
ugrep -z PATTERN archive.tgz
This greps each of the archived files to display PATTERN matches with the archived filenames. Archived filenames are shown in braces to distinguish them from ordinary filenames. Everything else is the same as grep (ugrep has the same options and produces the same output). For example:
$ ugrep -z "Hello" archive.tgz
{Hello.bat}:echo "Hello World!"
Binary file archive.tgz{Hello.class} matches
{Hello.java}:public class Hello // prints a Hello World! greeting
{Hello.java}: { System.out.println("Hello World!");
{Hello.pdf}:(Hello)
{Hello.sh}:echo "Hello World!"
{Hello.txt}:Hello
If you just want the file names, use option -l (--files-with-matches) and customize the filename output with option --format="%z%~" to get rid of the braces:
$ ugrep -z Hello -l --format="%z%~" archive.tgz
Hello.bat
Hello.class
Hello.java
Hello.pdf
Hello.sh
Hello.txt
Tarballs (.tar.gz/.tgz, .tar.bz2/.tbz, .tar.xz/.txz, .tar.lzma/.tlz) are searched as well as .zip archives.
You can mount the TAR archive with ratarmount and then simply search for the pattern in the mounted view:
pip install --user ratarmount
ratarmount large-archive.tar mountpoint
grep -r '<pattern>' mountpoint/
This should be much faster than iterating over each file and printing it to stdout, especially for compressed TARs.
Here is a simple comparison benchmark:
function checkFilesWithRatarmount()
{
local pattern=$1
local archive=$2
ratarmount "$archive" "$archive.mountpoint"
'grep' -r -l "$pattern" "$archive.mountpoint/"
}
function checkEachFileViaStdOut()
{
local pattern=$1
local archive=$2
tar --list --file "$archive" | while read -r file; do
if tar -x --file "$archive" -O -- "$file" | grep -q "$pattern"; then
echo "Found pattern in: $file"
fi
done
}
function createSampleTar()
{
for i in $( seq 40 ); do
head -c $(( 1024 * 1024 )) /dev/urandom | base64 > $i.dat
done
tar -czf "$1" [0-9]*.dat
}
createSampleTar myarchive.tar.gz
time checkEachFileViaStdOut ABCD myarchive.tar.gz
time checkFilesWithRatarmount ABCD myarchive.tar.gz
sleep 0.5s
fusermount -u myarchive.tar.gz.mountpoint
Results in seconds for a 55 MiB uncompressed and 42 MiB compressed TAR archive containing 40 files:
Compression
Ratarmount
Bash Loop over tar -O
none
0.31 +- 0.01
0.55 +- 0.02
gzip
1.1 +- 0.1
13.5 +- 0.1
bzip2
1.2 +- 0.1
97.8 +- 0.2
Of course, these results are highly dependent on the archive size and how many files the archive contains. These test examples are pretty small because I didn't want to wait too long but they already show the problem. The more files there are, the longer it takes for tar -O to jump to the correct file. And for compressed archives, it will be quadratically slower the larger the archive size is because everything before the requested file has to be decompressed and each file is requested separately. Both of these problems are solved by ratarmount.