将变量添加到! jupyter 中的命令

Adding variable to ! command in jupyter

我有一个字符串存储在一个变量中。该字符串表示视频文件的路径。我想在 jupyter-lab 中 运行 这个命令:

path = 'video.mp4'
meta = !ffmpeg -i path

这会导致找不到文件。

如果我直接输入路径,就可以了,像这样:

meta = !ffmpeg -i video.mp4
print(meta)
['ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers',
 '  built with Apple clang version 11.0.3 (clang-1103.0.32.62)',
 '  configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-librsvg --enable-libtheora --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libsoxr --enable-libspeex --enable-libass --enable-libbluray --enable-lzma --enable-gnutls --enable-fontconfig --enable-libfreetype --enable-libfribidi --disable-libjack --disable-libopencore-amrnb --disable-libopencore-amrwb --disable-libxcb --disable-libxcb-shm --disable-libxcb-xfixes --disable-indev=jack --enable-opencl --disable-outdev=xv --enable-audiotoolbox --enable-videotoolbox --enable-sdl2 --disable-securetransport --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --enable-libdav1d --arch=x86_64 --enable-x86asm --enable-libx265 --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid',
 '  libavutil      56. 51.100 / 56. 51.100',
 '  libavcodec     58. 91.100 / 58. 91.100',
 '  libavformat    58. 45.100 / 58. 45.100',
 '  libavdevice    58. 10.100 / 58. 10.100',
 '  libavfilter     7. 85.100 /  7. 85.100',
 '  libavresample   4.  0.  0 /  4.  0.  0',
 '  libswscale      5.  7.100 /  5.  7.100',
 '  libswresample   3.  7.100 /  3.  7.100',
 '  libpostproc    55.  7.100 / 55.  7.100',
 "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':",
 '  Metadata:',
 '    major_brand     : isom',
 '    minor_version   : 512',
 '    compatible_brands: isomiso2avc1mp41',
 '    encoder         : Lavf58.45.100',
 '  Duration: 00:00:17.34, start: 0.000000, bitrate: 479106 kb/s',
 '    Stream #0:0(eng): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv420p, 2160x3840 [SAR 1:1 DAR 9:16], 479126 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)',
 '    Metadata:',
 '      handler_name    : VideoHandle',
 '    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 129 kb/s (default)',
 '    Metadata:',
 '      handler_name    : SoundHandle',
 'At least one output file must be specified']

除了将路径存储在变量中之外,我如何才能保留此功能?我不想使用子流程,因为我想将元数据的输出存储在一个变量中。

您可以使用以下内容:

path = 'video.mp4'
meta = !ffmpeg -i $path

在变量名两边使用花括号强制变量插值:

path = 'video.mp4'
meta = !ffmpeg -i {path}